Https://www.codecademy.com/courses/introduction-to-javascript/projects/build-a-library

https://www.codecademy.com/courses/introduction-to-javascript/projects/build-a-library
Hello guys i was able to complete my library project but console returns
true
NaN
true
NaN
and i found out my subclass appear in red instead of blue(Book and Movie wont turn blue) which i identified after watching the project video. My work looked ok comparing it to the video.
Please someone should help me figure out the problem
this is what i did…

class Media {

constructor(title) {

this._name = title;

this._isCheckedOut = false;

this._ratings = [];

}

get title() {

return this._title;

}

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 sum = this.ratings.reduce(function(a, b) {

    a + b;

  })

  return sum / this.ratings.length;

}

addRating(value) {

  this._ratings.push(value);

}

}

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(title, director, 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(‘Speed’, ‘Jan de Bont’, 116);

speed.toggleCheckOutStatus();

console.log(speed.isCheckedOut);

speed.addRating(1);

speed.addRating(1);

speed.addRating(5);

console.log(speed.getAverageRating());

It would appear that method is working correctly since it logs a boolean. Same can be said for the other line below.

The fact that it is logging NaN means that somewhere in the program a non-number value has been employed in some maths, such as multiplication or subtraction.

1 Like

Yes thanks!
The problem was from the getaverageRating() method…

1 Like