Classes - Build a Library project

Here is my code -

Here is the video help on this project -

After the project, I compared my code with that of the code on the video help.
My two questions-

  1. This is about addRating method.
    My method -
    addRating(num){
    this._ratings.push(num);
    }
    In the video, the push is done on the getter as in this.ratings.push(num).
    Why is the add done on the getter when the actual property(this._ratings) needs to be updated?

  2. What is wrong with my getAverageRatings method?
    Can you please point out the defect in my way of doing this?
    getAverageRating(){
    const avgRating = this.ratings.reduce((acc,curVal)=>{
    return Math.floor(acc + curVal / this.ratings.length);
    })
    return avgRating;
    }

Thank you
Gopal

getAverageRating(){
const avgRating = this.ratings.reduce((acc,curVal)=>{
return Math.floor(acc + curVal / this.ratings.length);
})
return avgRating;
}

We should be completing the sum before dividing.

 return this.ratings.reduce((a, b) => a + b) / this.ratings.length
addRating(num){
this._ratings.push(num);
}

Lets say our setter looks like this…

get ratings () {
    return this._ratings
}
set ratings (value) {
    this._ratings.push(value)
}
addRating (rating) {
    this.ratings = rating
}

That way the only methods to refer to the backing variables are getter and setter.

@mtf
Thank you for the prompt response.
I understood your explanation on getAverageRatings method.
But I am afraid I did not understand the explanation on addRatings.
Yes, it makes sense to have the only getter and setter methods access the variables directly.
But then in this case we dont have a setter for ratings because we do NOT want to replace the array on ratings. We want to add new elements to the existing array of ratings.
Hence, we have the addRating method.
But we need to add the rating on the variable in the constructor.
How can we add the rating to the variable via a getter which does not update the variable?
Hope I made sense.

Regards
Gopal

But we’re not. We’re pushing the value sent to the setter. Consider this mockup…

class Media {
  constructor(name) {
    this._name = name;
    this._ratings = []
  }
  get ratings () {
    return this._ratings
  }
  set ratings (value) {
    this.ratings.push(value)    //  note that we use the getter
  }
  addRating (rating) {
    this.ratings = rating
  }
}
book = new Media('Sleeping Island')
book.addRating(8)
console.log(book.ratings)    //  [ 8 ]
book.addRating(9)
console.log(book.ratings)    //  [ 8, 9 ]

Thank you.
I understand.

1 Like

Hi can you please help check my code. I dnt know how am suppose to work aound getting addRatings to be between 1&5, also the catalog class seems to me like same as Media class. since it will have all properties of the Media. so what are my suppose to do with the Catalog class? thanks class Media {
constructor(AllMedia) {
this._title = ‘title’;
this._isCheckedOut = false;
this._ratings = ;
}
get title(){
return this._title;
}
get isCheckedOut() {
return this._isCheckedOut;
}
get ratings() {
return this._ratings;
}
toggleCheckOutStatus(isCheckedOut) {
if(isCheckedOut === true) {
return false;
} else {
return true;
}
}
getAverageRating() {
let ratingSum = this.ratings.reduce((accumulator, rating) => accumulator + rating, 0);
let ratingLength = this.ratings.length;
let average = ratingSum/ratingLength;
return average;
}
addRatings(rate) {
this.ratings.push(rate)
}
set isCheckedOut(newIscheckedOut) {
this._isCheckedOut = newIsCheckedOut;
}
}
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(director,title, runTime) {
super(title);
this._director = ‘director’;
this._runTime = runTime;
}
get director() {
return this._director;
}
get runTime() {
return this._runTime;
}
}
class CD extends Media {
constructor(artist, title, songs) {
super(title);
this._artist = ‘artist’;
this._songs = [‘song’, ‘another’];
}
get artist() {
return this._artist;
}
get songs() {
return this._songs;
}
shuffle() {
let randomSongs = Math.random(songs)
return randomSongs;
}
}
const historyOfEverything = new Book(‘Bill Bryson’, ‘A Short History of Nearly Everything’, 544);
historyOfEverything.toggleCheckOutStatus();
console.log(historyOfEverything.toggleCheckOutStatus());
historyOfEverything.addRatings(4);
historyOfEverything.addRatings(5);
historyOfEverything.addRatings(5);
console.log(historyOfEverything.getAverageRating());

const speed = new Movie(‘Jan de Bont’, ‘Speed’, 116);
speed.toggleCheckOutStatus();
console.log(speed.isCheckedOut);
speed.addRatings(1)
speed.addRatings(1)
speed.addRatings(5)
console.log(speed.getAverageRating());