Build a Library project

Hello, I’m on the Learn Intermediate JavaScript course and trying to solve the Build a Library project of the Classes lesson. (https://www.codecademy.com/courses/learn-intermediate-javascript/projects/build-a-library)

I tried to solve the project without following the steps or watching Matt’s video. I have a few questions:

  1. Before seeing the solution I used this for the toggleCheckOutStatus() method:
toggleCheckOutStatus() {
this.isCheckedOut ? false : true;
}

I thought that using the ternary operator would work just fine for me, although it doesn’t, the checkout status never changes, any idea why?

  1. I noticed in Matt’s video that he doesn’t prepend an underscore _ to the variables of the .getAverageRating() , .toggleCheckOutStatus() , and .addRating() methods . To my understanding, this is because we’ve already declared the necessary setters and getters before. Does this mean that we should use an underscore _ only in the getters and setters declaration and not in the other methods of the class?

If you’re trying to change the value of this.isCheckedOut
then you could do
this.isCheckedOut = this.isCheckedOut ? false : true;
(the code you had didn’t change the value of this.isCheckedOut )

Yes, if you have a getter and setter for a property, then you’d use the getter or setter outside the class, rather then using the property directly.

2 Likes

Thank you! I thought the ternary operator was returning a true or false value.

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