Build a Library, getAverageRating does not function

I have struggled to proceed on Build a Library project for some time now, as the task 19 revealed that my .getAverageRating() does nothing when called upon.

Here is my current progress - I have tried to mix and match various implementations that I’ve come across, but none of them seem to result in anything different.

class Media {
  constructor(title) {
    this._title = title;
    this._isCheckedOut = false;
    this._ratings = [];
  }

  get title() {
    return this._title;
  }
  get isCheckedOut() {
    return this._isCheckedOut;
  }
  get ratings() {
    return this._ratings;
  }

  set isCheckedOut(toggleCheckOutStatus) {
    this._isCheckedOut = toggleCheckOutStatus;
  }

  toggleCheckOutStatus() {
    this._isCheckedOut = !this._isCheckedOut;
  }

  getAverageRating() {
    /*let ratingsSum = this._ratings.reduce((currentSum, rating) => currentSum + rating, 0);
    const lengthRatings = this.ratings.length;
    return ratingsSum / lengthRatings;*/
    let ratingsSum = this._ratings.reduce((currentSum, rating) => currentSum + rating, 0);
    let ratingsLength = this._ratings.length;
    let average = ratingsSum / ratingsLength;
    this._ratings = average;
    return this._ratings;
  }

  addRating(ratings) {
    this._ratings.push(ratings);
  }
}

class Book extends Media {
  constructor(author, title, pages) {
    super(title);
    this._author = author;
    this._pages = 0;
  }

  get author() {
    return this._author;
  }
  get pages() {
    return this._pages;
  }
}

class Movie extends Media {
  constructor(director, title, runTime) {
    super(title);
    this._director = director;
    this._runTime = 0;
  }

  get director() {
    return this._director;
  }
  get runTime() {
    return this.runTime;
  }
}

const historyOfEverything = new Book('Bill Bryson', 'A Short History of Nearly Everything', 544);
historyOfEverything.toggleCheckOutStatus();
historyOfEverything.addRating(8, 1, 2);
historyOfEverything.getAverageRating();

console.log(`This title is ${historyOfEverything.isCheckedOut ? 'available' : 'unavailable'} and it has a rating of ${historyOfEverything._ratings}`);

What am I missing/doing wrong here?

here:

historyOfEverything.getAverageRating();

you call getAverageRating method, which returns the average rating, but you do nothing with the returned value?

I don’t fully understand what you mean.

I tried to save the function call to a variable, but it fails to calculate the assigned ratings. What ever I do, it either returns the first value of the ratings array (which is 8), or crashes into an error (which I can’t no longer reproduce, sigh…)

but does not reflect the code I see.

here:

historyOfEverything.addRating(8, 1, 2);

you attempt to add three ratings to the array, but if we inspect the ratings:

  addRating(ratings) {
    this._ratings.push(ratings);
    console.log(this._ratings);
  }

we can see that only a single rating is added. I think JavaScript should throw an error here, given you have positional argument mismatch

it doesn’t return the first value, it does really return the average (of the single item in average array)

1 Like

Oh now I feel a bit stupid - how did I miss that… :sweat_smile:

I separated the historyOfEverything.addRating(x); calls to three separate instances instead of trying to push three properties in at once (since it only expects one) and it works now, as expected.

Thank you!

its absolutely not stupid. I had to debug this issue, I started by logging some values within your getAverageRating method:

console.log(ratingSum, ratingLength);

That gave me a lot of information, which made me take a look at how the ratings where added. If you take anything away from this answer (which I hope you do haha) it should be about debugging in general, not this specific issue you had.

you could also use the spread syntax/operator (...), as demonstrated here with rest parameters:

https://javascript.info/rest-parameters-spread