Classes Project

Hi there,

Having trouble with my code. I keep getting an error when I try to use the inherited addRating method with my book class. Does anyone know where I’m going wrong?

Thanks in advance!

https://www.codecademy.com/paths/web-development/tracks/webdev-intermediate-javascript/modules/learn-javascript-classes/projects/build-a-library

class Media {
  constuctor(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;
  } 
  
  addRating(value) {
    this.ratings.push(value);
  }
  
  getAverageRating() {
    let ratingSum = this.ratings.reduce((accumulator, rating) => accumulator + rating);
    return ratingSum / this.ratings.length;
  }
}

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('Bill Bryson', 'A Short History of Nearly Everything', 544)

historyOfEverything.toggleCheckOutStatus();

console.log(historyOfEverything.isCheckedOut)

historyOfEverything.addRating(4);
historyOfEverything.addRating(5);
historyOfEverything.addRating(5);

And here’s the error message:

/home/ccuser/workspace/learn-javascript-classes-build-a-library/app.js:28
this.ratings.push(value);
^

TypeError: Cannot read property ‘push’ of undefined
at Book.addRating (/home/ccuser/workspace/learn-javascript-classes-build-a-library/app.js:28:17)
at Object. (/home/ccuser/workspace/learn-javascript-classes-build-a-library/app.js:75:21)
at Module._compile (module.js:571:32)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)

so this.ratings is undefined, how could that be?

this.ratings is the getter, so we could see if the code gets there:

get ratings() {
    console.log('getter ratings', this._ratings)
    return this._ratings
  }

good, the code gets there. But this._ratings is undefined. Where is this._ratings defined?

In the constructor of Media, so lets go there next:

constuctor(title){
   console.log('media constructor');
}

that is weird, no output

what if you continue this approach? See if the Book constructor does get called?

If the book constructor does get called, and the Media constructor does not, you narrowed down your problem to two lines

1 Like

You lost me here if I’m honest, surely this is defined, otherwise the checkout method wouldn’t have worked either…

you got “lucky” there.

if you comment out the toggleCheckoutStatus:

//historyOfEverything.toggleCheckOutStatus();

you will see that:

console.log(historyOfEverything.isCheckedOut)

gives undefined. From which we can draw two conclusions:

the constructor is not working
the _isCheckedOut proprety got initialized in toggleCheckOutStatus method

Ahhhh Ok. Thanks! I’ll keep looking

you made a small typo, but i wanted you to find it. I hoped you arrived at that conclusion after noticing your constructor doesn’t get called :wink:

I just found it! Thanks for your help and thanks for not telling me that straight away. STUPID TYPOS!!!

i hope it also taught you something about debugging. :slight_smile: And after “completing” codecademy, using a text-editor with spell check also helps

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.