Classes - Build library stuck

On the project where we must create a class media with subclasses book, movie, cd. I get an error on the addRating method.

Heres my code so far

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

  get title(){
    return this._title;
  }

  get ratings(){
    return this._ratings;
  }

  get isCheckedOut(){
    return this._isCheckedOut;
  }

  toggleCheckedOutStatus(){
    if(this._isCheckedOut === true){
      this._isCheckedOut = false;
    }else {
      this._isCheckedOut = true;
    }
  }

  getAverageRating(){
    let ratSum = this._ratings.reduce((currentSum, rating) => currentSum + rating, 0) / this._ratings.length;

    return ratSum.toFixed(2);

  }

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

  set isCheckedOut(checked){
    this.isCheckedOut = checked;
  }
}

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

  get author(){
    return this._author;
  }

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

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

  get director(){
    return this._director;
  }

  get runTime(){
    return this._runTime;
  }
}

const historyOfEverything = new Book('A Short History of Nearly Everything', 'Bill Bryson', 544);
historyOfEverything.toggleCheckedOutStatus();
console.log(historyOfEverything.isCheckedOut);
historyOfEverything.addRating([4,5,5]);
console.log(historyOfEverything.getAverageRating());

I get the error TypeError: Cannot read property ‘push’ of undefined
at Book.addRating

Hello, @blogpro21075, and welcome to the forums.

Could you post your entire code? Be sure to format it properly using the preformatted text button </>. See this post for more details on formatting code.

updated with entire code

1 Like

Well, if you add in a console.log() to inspect the value in question, you’ll find something interesting:

Try it, and compare the output to what you would expect.

Hint

Notice anything missing from the output?

this: Book { _author: ‘Bill Bryson’, _pages: 544, _isCheckedOut: true }

Where are _title and _ratings?
Why would they be missing?
Where is the code that is supposed to add those properties?
Is anything amiss there?

Thanks! I didn’t know we needed to list title and ratings in the extended constructor since it was already in the super.

You don’t. Look closely at the constructor for your Media class:

Hint

The properties in this constructor were not added due to the misspelling. :wink: