Build a library

I am having an issue with this project particularly the .toggleCheckOutStatus method.
My thinking is that the _isCheckedOut property is initialized to false. Ok fine. So, if I call the toggleCheckOutStatus method/function twice, shouldn’t the results in the log go from false to true and back to false??
I imagine the setter should access the property when the toggleCheckOutStatus method is called. However, I am unable to figure out how to do this.
Any assistance would be greatly appreciated.
Thank you.

If you toggle twice, it would become false again, I think you are right.

It would be helpful to see your code, so we know how you’ve attempted it already. From that, we can suggest ways you could adjust your program. :slight_smile:

class Media {
  constructor (title) {
    this._title = title;
    this._ratings = [];
    this._isCheckedOut = false;
  }
  get title () {
    return this._title;
  }
  get ratings () {
    return this._ratings;
  }
  get isCheckedOut () {
    return this._isCheckedOut;
    }
  get averageRating () {
    const x = this._ratings.reduce((accumulator, currentvalue) =>{
      return accumulator + currentvalue;
    }, 0)
    return x/(this._ratings.length);
  }
  addRating(xy) {
    this._ratings.push(xy);
  }
  set checkedOutStatus (value) {
    this._isCheckedOut = value;
  }
  toggleCheckOutStatus () {
    let x = !this.isCheckedOut;
    return this.checkedOutStatus = x;
     }
}
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._runtime = runtime;
    this._director = director;
  }
  get runtime() {
    return this._runtime;
  }
  get director() {
    return this._director;
  }  
}
class CD extends Media {
  constructor (title, artist, songs) {
    super(title);
    this._songs = songs;
    this._artist = artist;
  }
  get songs() {
    return this._pages;
  }
  get artist() {
    return this._artist;
  }
}
const historyOfEverything = new Book('A Short History of Nearly Everything', 'Bill Bryson', 544);
\
console.log(historyOfEverything.isCheckedOut);
\
historyOfEverything.toggleCheckOutStatus;
\
console.log(historyOfEverything.isCheckedOut);

The code above outputs ‘false’ to the console twice.

toggleCheckOutStatus is a method, but you’re not calling it like one…

You’re missing two very simple characters from your attempt to toggle the status. :slight_smile:

wow… :unamused:
I can’t believe I missed that. Thank you!

1 Like

No worries, sometimes the obvious ones are the hardest to spot! :slight_smile:

Hi, I am trying to make the below code work:

getAverageRating() {
let average = this.ratings.reduce((accumulator, rating) =>
accumulator + rating) / this.ratings.length;
return average / this.ratings.length;
}

When I run below code:
console.log(historyOfEverything.getAverageRating)

I get this on the console:
[Function: getAverageRating]

I know this code is not wrong since I had to check the solution after many tries but somehow it doesn´t log the average number to the console. Can you help?

Hey there, welcome to the forums.

You’re saying that your code is absolutely correct, but you’re not getting the expected outcome.

The simplest explanation is that either your code is wrong, or your expectation of what the outcome should be is wrong.

In this case, your code is at fault.

Look closely at what you’re logging and how that might explain the result you’re getting:

console.log(historyOfEverything.getAverageRating)

Hello and thank you!

What I understand is if you want to only call and not log to the console you write with brackets -> getAverageRating ()
if you want to log the solution to the console -> getAverageRating
I am trying to log an average number of an array [4,5,5] like this.

So yes I have no idea what´s wrong, I spend whole afternoon yesterday and checked every single line of code I wrote, syntax…etc.

I can copy paste the whole thing if necessary.

Not exactly sure what that means, but try a little experiment:

const sayHi = name => {
  return `Hello, ${name}!`;
}

console.log(sayHi);
console.log(sayHi('codewhiz69176'));

What does each console.log() print?

2 Likes

Ok I think first one will log Hello, ${name}! as it is (since name parameter don´t have a value defined)
Second one will log Hello, codewhiz69176!

So you mean I need to add a value in the below code?
console.log(historyOfEverything.getAverageRating)

You could run it yourself, and see, but the here is the output:

[Function]
Hello, codewhiz69176!

Any time you want a function to execute you have to include the (). If the function requires parameters, you must list arguments for the parameters inside the parenthesis in the function call. If the function requires no parameters, you simply include empty () to call the function.

No, but if you want to print the return value of the function to the console, you have to call the function:

console.log(historyOfEverything.getAverageRating()); //the return value of your
//function will be the argument provided to the console.log() function, and will
//be printed to the console.
1 Like

I remember trying with paranthesis but guess i was putting it the wrong way, thank you so much for help, now it works.

2 Likes