Build a Library Exercice

Hi Everybody !

I follow the Web Developpment Path.

Now, I did and finish ‘Build a Library’ exercice but my JS code doesn’t run correctly.

I don’t know why :frowning: !!! I’m stucking !

So, who can help me to debug my code please ? :slightly_smiling_face:

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(accumulator, rating) => accumulator + rating);
    return ratingsSum / this.ratings.length;
  }

  addRatings(value) {
    this.ratings.push(value);
  }
}


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, runttime) {
    surper(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(6);.

console.log(historyOfEverything.getAverageRating());


const speed = new movie('Jan de Bont', 'Speed', 116);

speed.toggleCheckOutStatus();

console.log(speed.isCheckedOut);

speed.addRating(1);
speed.addRating(1);
speed.addRating(5);

console.log(speed.getAverageRating());

Your code is for the most part fine logically and structurally, but you’ve made several typos. For example, when declaring a class you do not need the = sign like you do with Media at the beginning. Also there are some rogue characters here and there that is stopping it from running. Specifically, check your addRatings() calls, and also check your constructor for movies. There are a couple more typos as well so just read through your code again and keep an eye out for missing characters. If you still can’t get it, post up the code again with your edits and I’ll point out all the remaining edits.

Logically it’s very good though! Just some small errors.

2 Likes

Hi Adam,

thanks for your help.

I have tried to find and solve my bugs but code don’t run yet :frowning:

Can you help me ?

Thanks.

Tony

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((accumulator, rating) => accumulator + rating);
    return ratingsSum / this.ratings.length;
  }

  addRatings(value) {
    this.rating.push(value);
  }
}



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.toggleCheckOutStatus();

console.log(historyOfEverything.isCheckedOut);


historyOfEverything.addRating(4);
historyOfEverything.addRating(5);
historyOfEverything.addRating(6);.

console.log(historyOfEverything.getAverageRating());


const speed = new movie('Jan de Bont', 'Speed', 116);

speed.toggleCheckOutStatus();

console.log(speed.isCheckedOut);

speed.addRating(1);
speed.addRating(1);
speed.addRating(5);

console.log(speed.getAverageRating());

Just here you have a . at the end of the function, that’s causing one error. Also every time you’ve called addRatings() you have written addRating() instead.

And finally here you have used this.rating however your getter method is ratings therefore it should be this.ratings.push(value) and if you’ve fixed all that then you should find it works! Nothing wrong with the logic at all, just small typos that are really easy to overlook, it happens to everybody.

Hi,

tanks for your help. :slightly_smiling_face:

1 Like