Objects JavaScript


#1

Hello, can you please help debugging, why my average rating is undefined?

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(checkedOut) {
    this._isCheckedOut = checkedOut;
  }
toggleCheckedOutStatus() {
  this._isCheckedOut = !this._isCheckedOut;
}
addRating(ratings){
this._ratings.push(ratings);
}

getAverageRating(){
let lengthOfArray = this._ratings.length;
let ratingsSum = this.ratings.reduce((currentSum, rating) => currentSum + rating, 0);
let average = Math.round(ratingsSum / lengthOfArray);
this._ratings = average;
}

}

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 Bryson', 'A Short History of Nearly Everything', 544)

historyOfEverything.toggleCheckedOutStatus();
console.log(historyOfEverything.isCheckedOut);
historyOfEverything.addRating(4)
historyOfEverything.addRating(5)
historyOfEverything.addRating(5)
const b = historyOfEverything.getAverageRating();
console.log(b)
const speed = new Movie('Jan de Bont', 'Speed', 116);
speed.toggleCheckedOutStatus();
console.log(speed.isCheckedOut);
speed.addRating(1);
speed.addRating(1);
speed.addRating(5);

#2

getAverageRating doesn’t return anything? So the default (undefined is default) is returned

also, this:

this._ratings = average;

Is problematic, if you then call getAverageRating a second time, this.ratings will be an integer, integer doesn’t have a .length property, so you will get an error.


#3

Awesome. Thanks.
I fixed it all, by removing this._ratings = average :slight_smile:

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(checkedOut) {
    this._isCheckedOut = checkedOut;
  }
toggleCheckedOutStatus() {
  this._isCheckedOut = !this._isCheckedOut;
}
addRating(ratings){
this._ratings.push(ratings);
}

getAverageRating(){
let lengthOfArray = this._ratings.length;
let ratingsSum = this.ratings.reduce((currentSum, rating) => currentSum + rating, 0);
return Math.round(ratingsSum / lengthOfArray);
}

}

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 Bryson', 'A Short History of Nearly Everything', 544)

historyOfEverything.toggleCheckedOutStatus();
console.log(historyOfEverything.isCheckedOut);
historyOfEverything.addRating(4)
historyOfEverything.addRating(5)
historyOfEverything.addRating(1)
historyOfEverything.addRating(5)
const b = historyOfEverything.getAverageRating();
console.log(b)
const speed = new Movie('Jan de Bont', 'Speed', 116);
speed.toggleCheckedOutStatus();
console.log(speed.isCheckedOut);
speed.addRating(1);
speed.addRating(1);
speed.addRating(5);

#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.