class Media {
constructor(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 ratingsSum =
this.ratings.reduce = ((accumulator, rating) =>
accumulator + rating);
return ratingsSum / 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.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);
console.log(speed.getAverageRating());
There is the error, let ratingSum is defined as an empty variable.
If you then return a division of that variable it returns a NaN.
Also:
get pages() {
return this.pages;
}
}
is missing an _
Please see my comments added to your code. As @janneslohmeijer pointed out, you are missing the underscore in a few places in your code when referring to the backing variables. Look at your addRating
and get pages
functions.
It appears that maybe this line this.ratings.reduce = ((accumulator, rating) => accumulator + rating);
was intended to be the value assigned to ratingsSum
That will work, but there should not be an =
after reduce
.
2 Likes
Thank you, that was helpful!
1 Like