Build a Library


#1

Hello,

I would like to get an understanding of something. Here is the lesson I’m on:

https://www.codecademy.com/courses/learn-javascript-classes/projects/build-a-library?r=master

The question I have pertains to an error I get when I run this code:

class Media{
constructor(title) {
this._title = title;
this._ratings = [];
this._isCheckedOut = false;
}
get title(){
return this._title;
}
get isCheckedOut(){
return this._isCheckedOut;
}
get ratings(){
return this._ratings;
}

toggleCheckOutStatus(){
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(title, author, pages){
    super(title);
    this._pages = pages;
    this._author = author;
    
  }
  
  get pages () {
    return this._pages;
  }
  
  get author () {
    return this._author;
  }
}

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;
  }
}

class CD extends Media {
  constructor (artist) {
    super (title);
    this.songs = [];
  }
}

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

historyOfEverything.toggleCheckedOutStatus();
console.log(historyOfEverything.isCheckedOut);
historyOfEverything.addRating(4);
historyOfEverything.addRating(5);
historyOfEverything.addRating(5);
historyOfEverything.getAverageRating();

const speed = new Movie ('Speed', 'Jan de Bont', 116);
speed.toggleCheckOutStatus();

The error I receive is :

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

TypeError: historyOfEverything.toggleCheckedOutStatus is not a function
    at Object.<anonymous> (/home/ccuser/workspace/learn-javascript-classes-build-a-library/app.js:75: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: 

Why is it that I get:
TypeError: historyOfEverything.toggleCheckedOutStatus is not a function when I run
speed.toggleCheckOutStatus();?

isn’t toggleCheckOutStatus() a function?
It works if I run it without the () at the end like this : speed.toggleCheckOutStatus;

It is not a key , nor is there a getter for it. I would like to understand how this works.

Thanks in advance everyone.


#2

If you do a text search in your code, toggleCheckedOutStatus only has one match, the place where you use it. In other words, it wasn’t defined anywhere.

Also, the typeof operator could have told you what exactly it was – and if it didn’t match expectations you’d double check the thing that should have made it something else


#3

Ah ok, I see where my lack of understanding comes in here.
When I do

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

this doesn’t define toggleCheckOutStatus() does it?


#4

It does.


#5

Ah, so then doing

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

doesn’t actually make it a function then. :sweat:


#6

It does.

Read over my first response again. The part about something only existing in one place in the code

Subtle as it may be, we make these mistakes, we won’t ever stop, but we can get better at recognising them.


#7

ok, let me think, let me think.


#8

If I create a function f, and call it – then f will be mentioned twice in the code.
Once when defining it and once when using it.

You only have that symbol once in your code, therefore…

And also, you can inspect your object to find out what it provides. That’s what I did.
(This was supposed to be a function, it’s not, well let’s look at the object then)


#9

Let’s see, I created it in the Media object.

but didn’t I call in on historyOfEverything? Oh wait…

OMG…lol I had it typed wrong… that helped man. THANKYOU. I had toggleCheckOutStatus instead of toggleCheckedOutStatus in the object. :sweat_smile:


#10

Defining it and using it requires two mentions of a symbol

If you only mention it once, then you must therefore either not be defining it, or you must not be using it.


#11

Right. And it’s easy to fix it and move on.

But you’ll do it again. So methodically figuring it out is the important part.


#12

Thank you for helping me on getting my thinking patterns together on this.


#13

Don’t worry about it. It’s hard to reason about things when you don’t know what’s up and what’s down. Need something solid to stand on or you’ve got too many unknown variables to be able to solve the equation


#14

Yes sir, I’m so new to this, but I feel everyday like I can actually be successful at learning this. One step closer to the end-goal.


#15

Meal Maker Fun and Sadness. Please help
Is the very same thing you know. Dig back to where it should have come from. A lot of debugging boils down to just that. (having a mental understanding of what should happen, and then comparing with what does happen)

console.log helps with this. (use english text to describe what’s printed, or you end up with lots of values being printed out with no context)

Completion engines also help reduce the number of times you make typos – if you start typing something and there’s no match then you immediately know there’s a typo.


#16

Yes, I had a hard time with that one. I still need to go back to it. I skipped it thinking that other exercises would give me the understanding as to how to attack it. I will go back now.


#17

Spoiler: it’s a typo


#18

lol NOT AGAIN ! Me and my eyes.

I got so lost in all of the getters and setters and properties.

I think I’m going to start over and start from scratch on this one.


#19

A post was split to a new topic: Build a library