Build a Library - getAverageRating

Hey guys. My getAverageRating method is not working as it should. Can you tell me why? The walkthrough video link is broken.
The exercise is this one: Build a Library

Thanks!

My code:

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(check) {
    this._isCheckedOut = check;
  }
  
  toggleCheckOutStatus() {
		this._isCheckedOut = !this._isCheckedOut;
  }
  
  getAverageRating() { 
    let ratingsSum = this._ratings.reduce((currentSum, rating) => currentSum + rating, 0);
    const lengthOfArray = this._ratings.length;
    return ratingsSum / lengthOfArray;
  }
  
  addRating(rating) {
    this.ratings.push(rating);
  }
}

class Book extends Media {
  constructor(author, title, pages) {
    super(title);
    this._author = author;
    this._pages = pages;
  }
  
  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 = runTime;
  }
  
  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();
console.log(historyOfEverything.isCheckedOut);
historyOfEverything.addRating(4, 5, 5);
console.log(historyOfEverything.getAverageRating());

const speed = new Movie('Jan de Bont', 'Speed', 116);
speed.toggleCheckOutStatus();
console.log(speed.isCheckedOut);
speed.addRating(1, 1, 5);
console.log(speed.getAverageRating());
1 Like

Let’s look at the minimalist approach…

    return this.ratings.reduce((a, b) => a + b) / this.ratings.length;
    //         get                                    get

Now let’s look at the addRating method as a possible chink in the armor…

    this._ratings.push(rating);
    //  set
2 Likes

…I still don’t get it.

const historyOfEverything = new Book('Bill Bryson', 'A Short History of Nearly Everything', 544);
historyOfEverything.toggleCheckOutStatus(); 
console.log(historyOfEverything.isCheckedOut);  //Outputs "true". OK.
historyOfEverything.addRating(4, 5, 5); //Outputs "4".  Not Ok.
console.log(historyOfEverything.getAverageRating());

const speed = new Movie('Jan de Bont', 'Speed', 116);
speed.toggleCheckOutStatus();
console.log(speed.isCheckedOut); //Outputs "true". OK.
speed.addRating(1, 1, 5); //Outputs "1". Not Ok
console.log(speed.getAverageRating());

Look at your addRating method. How many arguments does it accept?

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

Now look at this:

historyOfEverything.addRating(4, 5, 5);

and this:

speed.addRating(1, 1, 5);

If you’re still stuck, click the spoiler:

Your addRating method only accepts one argument, so only the first value is added. You need three separate statements calling the addRating method. Send one value each time:
For example:
speed.addRating(1);
speed.addRating(1);
speed.addRating(5);
This assumes you’ve already changed this.ratings.push(rating); to this._ratings.push(rating);

Of course! Now reading the instructions again, it says: “call it 3 times”… Speed reading is not something I advocate anymore.
Thanks for you patience!

1 Like

Hi there, it’s a nice shorthand interpretation of the syntax for that exercise, thanks. However, i didn’t get why it gave us (to novice) in the instruction of the example a grueling version, to get us entangled?

1 Like

One highly doubts any deliberate attempt to entangle learners so put that thought aside. Has the course already covered the unit on iterators?