Build a Library - Error

I am working on the JavaScript project "Build a Library. I don’t know why line 72 is giving me an error. I tested the code in other development environments but I can’t figure it out.

The error message is:

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

TypeError: historyOfEverything.toggleCheckOutStatus is not a function
at Object. (/home/ccuser/workspace/learn-javascript-classes-build-a-library/app.js:72:21)
at Module._compile (module.js:571:32)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)
at bootstrap_node.js:542:3

My current code is:

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;

}

addRating(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, 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(5);

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

Hi @raul.rovira
welcome to the forum!
The error message already gives a very clear hint: There is no method with the name toggleCheckOutStatus in your class Media. Take a close look at the spelling.
Please format your code in future posts, that makes it much easier for everyone to read it:

1 Like

Please note that JavaScript is case sensitive.

You have defined toggleCheckoutStatus, but you use it as so: toggleCheckOutStatus (notice that the O is capital). Case sensitivity is very important in JavaScript. That is why you are getting the error.

MYVAR is different from myvar, or even myVar.

1 Like

Thank you for pointing that out. I went back to CodeCademy to take a look and it worked. Usually, when the code does not work I find a spelling error. This one was hiding in plain sight.

Grateful for your help.

Raúl

Yep!

Spelling errors are quite painful, but if you can read the error JavaScript throws then it usually will lead to the discovery of a spelling mistake.

1 Like