Javascript projects-build-a-library

Hi All,

I am working through the ‘Build a Library’ project and came across a problem when calling toggleCheckoutStatus() on historyOfEverything. When running the code I get the below error message. I’m not sure why this is causing an error code and would appreciate advice :slight_smile:

/home/ccuser/workspace/learn-javascript-classes-build-a-library/app.js:66
historyOfEverything.toggleCheckoutStatus();
                    ^

TypeError: historyOfEverything.toggleCheckoutStatus is not a function
    
Please see my code below:

class Media {
constructor(title){
this._title = title;
this._isCheckout = true;
this._ratings = ;
}

get title() {
return this.title;
}
get isCheckout() {
return this.isCheckOut;
}
get ratings() {
return this.ratings;
}

toggleCheckOutStatus() {
this.isCheckOut = !this.isCheckOut;
}

getAverageRating(value) {
let ratingSum = this._ratings.reduce((accumulator, rating) => accumulator + rating);
return this.ratingSum / this._ratings.length;
}

addRating(inputValue) {
return this._rating.push(inputValue);
}

set isCheckOut(value) {
this._isCheckOut = value;
}
}

class Book extends Media {
constructor (title, author, pages) {
super(title)
this._author = author;
this._pages = pages;
}
get authour() {
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.isCheckOut)

To preserve code formatting in forum posts, see: [How to] Format code in posts

Consider the following issues:

  • You must be consistent in the capitalization of the variables. At times, you have used isCheckOut whereas in other places you have used isCheckout. To the compiler, they are not the same. Decide which of the two is the correct version as desired by you. Then, go through your code and change all names to the correct version.

  • Similarly, you wrote historyOfEverything.toggleCheckoutStatus() whereas in the Media class, you named the method as toggleCheckOutStatus

  • In the Book class, you spelled your getter as authour instead of author.

  • In the Media class, your getters are calling themselves instead of the properties. This leads to infinite loops. For Example,

// You wrote:
get title() {
    return this.title;
}

// It should be:
get title() {
    return this._title;
}

4 Likes

That’s very helpful thank you for the reply :slight_smile:

1 Like

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