[https://www.codecademy.com/paths/web-development/tracks/webdev-intermediate-javascript/modules/learn-javascript-classes/projects/build-a-library ]
I’m having troubles with the inheritance of the Media class by the Book class.
I believe I coded it right, then I watched the tutorial help video & it did the same as me. I also copied & pasted key variables to ensure that they were identical. it worked on the video, but not for me.
The trouble I’m having is that when the code tries to push a new value onto the end of the ratings array, the ratings array is undefined & so performing a push on undefined generates an error.
this.ratings.push(newRating);
^
TypeError: Cannot read property ‘push’ of undefined
ratings was defined in Media as such:
class Media {
constuctor(title) {
this._title = title;
this._ratings = ;
this._isCheckedOut = false;
}
get title() {
return this._title;
} …rest of the class }
Any help ? … Please!
Please post your full code
did you implement a getter for _ratings
property?
class Media {
constuctor(title) {
this._title = title;
this._ratings = [];
this._isCheckedOut = false;
}
get title() {
return this._title;
}
get isCheckedOut() {
return this._isCheckedOut;
}
get ratings() {
return this._ratings;
}
set isCheckedOut(checkOutStatus) {
this._isCheckedOut = checkOutStatus;
}
toggleChekOutStatus() {
this._isCheckedOut = !this._isCheckedOut;
}
getAverageRating () {
const reducer = (accumulator, currentValue) => accumulator + currentValue;
let ratingsSum = this.ratings.reduce(reducer);
let ratingsNum = this.ratings.length;
return ratingsSum/ratingsNum;
}
addRating(newRating) {
this.ratings.push(newRating);
}
}
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);
console.log(historyOfEverything);
historyOfEverything.toggleChekOutStatus();
console.log(historyOfEverything.isCheckedOut);
console.log(historyOfEverything.ratings);
console.log(historyOfEverything.title);
historyOfEverything.addRating(4);
historyOfEverything.addRating(5);
historyOfEverything.addRating(5);
console.log(historyOfEverything.getAverageRating());
I implemented the getter for ratings however, the problem seems to be that the super() has not initiated the media constructor. The toggleChekOutStatus works (even though I spelt it wrong, but consistently wrong
, so the media class is understood by the Book class.
seems you solved your own problem? Check the spelling of your Media constructor
method
The important lesson is here, if you find an issue that a method (like constructor) isn’t called, you need to learn how to debug it