Help with Build a Library project

Hello other JavaScript learners and programmers!

I’m having trouble with the Build a Library project, part of the Classes topic in the Learn JavaScript tree.

Here is my Code thus far:

class Media {
  constructor (title){
    this._title = title;
    this._ratings = [];
    this._isCheckedOut = false;
  }
  get title (){
    return this.title;
  }
  get isCheckedOut (){
    return this._isCheckedOut;
  }
  get ratings (){
    return this._ratings;
  }
  set isCheckedOut(isCheckedOut){
    return this._isCheckedOut
  }
  toggleCheckOutStatus(){
    this._isCheckedOut = !this._isCheckedOut
  }
  getAverageRating() {
    let ratingsSum = 
  this.ratings.reduce((accumulator, currentValue) => {
      return accumulator + currentValue;
    })
  }
  addRating(rating){ 
    this._ratings.push(rating);
  }
  //not sure about the syntax for both getAverageRating and addRating because it I dont know if it is calculating an average from the unset array ??
}
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 Bryton", "A Short History of Nearly Everything", 544)

historyOfEverything.toggleCheckOutStatus()

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


I think my problem is with the syntax of the getAverageRating method, or the addRating method, because it returns ‘undefined’ to the console - I’m struggling to understand where I’ve gone wrong !

Any advice would be massively appreciated, thank you in advance, C :upside_down_face:

Hi Callum,
the reason why you get ‘undefined’ is that you don’t return the reduced value from the method. You assign it to a variable instead.
If you would return the calculated sum it would not be the average value of all ratings, but the sum of it, so you have to slightly adjust what’s being returned from the reduce method.
After you added the ratings by calling the ‘addRating’ method, you should log the _ratings array. I guess the output is not what you expect it to be.