Javascript syntax partIII: build a library project, ratings not defined

You must select a tag to post in this category. Please find the tag relating to the section of the course you are on E.g. loops, learn-compatibility

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with.

the exercise link: Learn JavaScript Syntax: Classes | Codecademy

I got stuck in the getAverageRating method. I can print out the ratings array, and I can print out the ratings array length.
but when the getAverageRating method is called, it turned out the ratings is not defined. Would you please help on this issue. thanks a lot.

Hello @shibinzh, welcome to the forums! Can you please post your code, making sure to format it?

hi, codeneutrino,

Thanks a lot for your reply. below are my code:
class Media {
constructor (title, isCheckedOut, ratings=) {
this._title = title;
this._isCheckedOut = false;
this._ratings = ratings = ;
}

get title () {
return this._title;
}

get isCheckedOut () {
return this._isCheckedOut;
}

get ratings () {
return this._ratings;
}

set isCheckedOut (newIsCheckedOut) {
this._isCheckedOut = newIsCheckedOut;
}

toggleCheckOutStatus () {
this._isCheckedOut = !this._isCheckedOut;

}

getAverageRating () {
let ratingsSum = this.ratings.reduce((currentSum, rating) => currentSum + rating, 0);
return ratingsSum/(ratings.length);

}

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 Everythign’,544);
historyOfEverything.toggleCheckOutStatus();
console.log(historyOfEverything.isCheckedOut);
historyOfEverything.ratings = ;
historyOfEverything.addRating(4);
historyOfEverything.addRating(5);
historyOfEverything.addRating(5);
console.log(historyOfEverything.ratings);
console.log(historyOfEverything.ratings.length);
//historyOfEverything.getAverageRating();
const speed = new Movie(‘Jan de Bont’, ‘Speed’, 116);
speed.toggleCheckOutStatus();
console.log(speed.isCheckedOut);
speed.addRating(1);
speed.addRating(1);
speed.addRating(5);
speed.getAverageRating();

thanks a lot.
regards,

I found a problem in the getAverageRating function:

On line with return, you’re missing a this
ratings.length
should be
this.ratings.length


In this constructor, this seems confusing to me:


Also, Next time, please post your code using the </> button and paste the code between the ``` and ``` so that the code in the post keeps it's formatting. It will make your code easier to read in the post.

hi, Janbazant,

Thanks a lot for your great help. Now the code is working as it supposed to be.
I removed the “= ” in the code.

will use the right format.

Thanks again.

regards,