What is wrong about the build a libary code, thanks

this is my code to build a library, but something goes wrong when I run the code
this is the link of the project building a library

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((currentSum,rating)=> currentSum + rating);
    return ratingsSum/this._ratings.length;
  }
  addRating(inputValue){
    this._ratings.push(inputValue);
  }
}

class Book extends Media {
   constructor(title, author, pages){
     super(title);
     this._author = author;
     this._ratings = pages;
     }

     get author(){
       return this._author;
     }

     get pages(){
       return this._pages;
     }
}
class Movie extends Media {
   constructor(title, director, runTime){
     super(title);
     this._director = director;
     this._runTime = runTime;
     }

     get director(){
       return this._director;
     }

     get runTime(){
       return this._runTime;
     }
     
}
class CD extends Media {
   constructor(title, artist, songs){
     super(title);

     this._artist = artist;
     this._songs = songs;
     }

     get artist(){
       return this._artist;
     }

     get songs(){
       return this._songs;
     }
}

const historyOfEverything = new Book('A Short History of Nearly Everything', 'Bill Bryson', 544);
historyOfEverything.toggleCheckOutStatus();
console.log(historyOfEverything.isCheckedOut);

historyOfEverything.addRating(4);
historyOfEverything.addRating(5);
historyOfEverything.addRating(5);
console.log(historyOfEverything.getAverageRating());

when run the code, error comes like this:

true
/home/ccuser/workspace/learn-javascript-classes-build-a-library/app.js:27
this._ratings.push(inputValue);
^

TypeError: this._ratings.push is not a function
at Book.addRating (/home/ccuser/workspace/learn-javascript-classes-build-a-library/app.js:27:19)
at Object. (/home/ccuser/workspace/learn-javascript-classes-build-a-library/app.js:83:21)
at Module._compile (module.js:571:32)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)

if we add a log to the method:

  addRating(inputValue){
    console.log(this._ratings);
    this._ratings.push(inputValue);
  }

we can see that this._ratings is a number. .push() is an array method.

where does the array become a number?

1 Like

Thank you for your method with which, I found the error lies in the Book extends Media
class Book extends Media {
constructor(title, author, pages){
super(title);
this._author = author;
this._ratings = pages;
}

the last line should be this._pages = pages instead of this._ratings = pages;
Thank you very much.

nice that you found the problem yourself :slight_smile: Teaching yourself this debug steps is so important

1 Like