JavaScript Classes - problem with functions


#1

I’m on the exercise called “Building a Library” https://www.codecademy.com/paths/web-development/tracks/webdev-intermediate-javascript/modules/learn-javascript-classes/projects/build-a-library

My code seems to be working properly if I just log the entire classes to the console. But whenever I’m trying to modify the content of a property with a function, the console shows “undefined”. I’m trying to do this:

const historyOfEverything = new Book('Bill Bryson', 'A Short History of Nearly Everything', 544)

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

And the rest of the code:

class Media {
  constructor(title) {
    this._title = title
    this._isCheckedOut = false
    this._ratings = []
  }
  
  get title() {
    return this._title
  }
  
  get isCheckedOut() {
    this._isCheckedOut
  }
  
  get ratings() {
    return this._ratings
  }
  
  set isCheckedOut(isCheckedOutIn) {
    this._isCheckedOut = isCheckedOutIn
  }
  
  toggleCheckOutStatus() {
    this._isCheckedOut = !this._isCheckedOut
  }
  
  getAverageRating() {
    let ratingsSum = this.ratings.reduce((currentSum, rating) => {return currentSum + rating})
    let lengthOfArray = this.ratings.length
    let avgRating = ratingsSum / lengthOfArray
  }
  
  addRating(rating) {
    this.ratings.push(rating)
  }
};

class Book extends Media {
  constructor(author, title, pages, isCheckedOut, ratings) {
    super(title, isCheckedOut, ratings)
    this._author = author
    this._pages = pages
  }
  
  get author() {
    return this._author
  }
  
  get pages() {
    return this._pages
  }
};

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

I appreciate any help, thank you.


#2

This method does not return anything, hence, undefined is the result.


#3

How silly of me…

I still have another problem with historyOfEverything.toggleCheckOutStatus() because it logs undefined to the console after console.log(historyOfEverything.isCheckedOut), but it shows the correct boolean value of _isCheckedOut when I log the whole object console.log(historyOfEverything)


#4

Missnig the return keyword.