I have struggled to proceed on Build a Library project for some time now, as the task 19 revealed that my .getAverageRating()
does nothing when called upon.
Here is my current progress - I have tried to mix and match various implementations that I’ve come across, but none of them seem to result in anything different.
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(toggleCheckOutStatus) {
this._isCheckedOut = toggleCheckOutStatus;
}
toggleCheckOutStatus() {
this._isCheckedOut = !this._isCheckedOut;
}
getAverageRating() {
/*let ratingsSum = this._ratings.reduce((currentSum, rating) => currentSum + rating, 0);
const lengthRatings = this.ratings.length;
return ratingsSum / lengthRatings;*/
let ratingsSum = this._ratings.reduce((currentSum, rating) => currentSum + rating, 0);
let ratingsLength = this._ratings.length;
let average = ratingsSum / ratingsLength;
this._ratings = average;
return this._ratings;
}
addRating(ratings) {
this._ratings.push(ratings);
}
}
class Book extends Media {
constructor(author, title, pages) {
super(title);
this._author = author;
this._pages = 0;
}
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 = 0;
}
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();
historyOfEverything.addRating(8, 1, 2);
historyOfEverything.getAverageRating();
console.log(`This title is ${historyOfEverything.isCheckedOut ? 'available' : 'unavailable'} and it has a rating of ${historyOfEverything._ratings}`);
What am I missing/doing wrong here?