I need some help with me program

whenever is log this code:

class Media {


  contructor(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(value) {
    this._isCheckedOut = value;
  }

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

  getAverageRating() {
    let ratingSum = 
    this.ratings.reduce((accumulator, rating) => accumulator + rating);
    return ratingSum / this.ratings.length;
  }

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

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);
historyOfEverything.addRating(5);
historyOfEverything.addRating(5);
console.log(historyOfEverything.getAverageRatings());


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

into the console this error shows up:


this is the link to my project in codecademy: https://www.codecademy.com/courses/learn-intermediate-javascript/projects/build-a-library

You do not have a property ‘ratings’ in your class. Check the properties you defined in your constructor.

i still dont understand can you please explain

The array you try to push to is called _ratings. Not ratings.

can you show me what the code should look like? I watched the project walkthrough 3 times and restarted the project three times and still don’t understand

Inyour constructor, you have this:

Therefore, in your method getAverageRating() you need to reference _ratings accordingly.
Rather than this:
this.ratings.reduce((accumulator, rating) => accumulator + rating);
you need to write this:
this._ratings.reduce((accumulator, rating) => accumulator + rating);

Same here:

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

It needs to be this._ratings.push(value);

I don’t know if your code has more errors, but that’s where the error you were thrown comes from.

get ratings() {
    return this._ratings;
  }

@mirja_t I think because of the presence of the getter, this.ratings also works and does not cause the undefined. If we comment out the getter, then you are correct that this.ratings will throw an error while this._ratings will work.

@abdulrehman13579 It was tricky to nail down what was causing the errors, but by using console.log statements for debugging, it became clear that the constructor of the parent wasn’t being called. You have two typos in your code:

You wrote:
contructor(title)

//  It should be:
constructor(title)

// You wrote:
console.log(historyOfEverything.getAverageRatings());

// It should be:
console.log(historyOfEverything.getAverageRating());
2 Likes

Oh, wow, you are right! Due to the very explicit error message ‘Cannot read property ‘push’ of undefined’, I thought the task was clear and easy and overlooked the getter. Should have double checked.

2 Likes

Thank you guys! This really helped, I appreciate it!

2 Likes