I am not able to find error

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(val){

  this.isCheckedOut = val;

}

toggleCheckOutStatus(){

  this.isCheckedOut = !this.isCheckedOut;

}

getAverageRating(){

  let sum = 0;

  for(let i=0;i<this.ratings.length;i++){

    sum+=this.ratings[i];

  }

  let avg = sum/this.ratings.length;

  return avg;

}

addRating(rate){

  this.ratings.push(rate);

}

}

class Book extends Media {

constructor(title,author,pages){

  super(title);

  this._author=author;

  this._pages=pages;}

  get author(){

    return this._author;

  }

  get pages(){

    return this._pages;

  }

}

class CD extends Media {

}

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.isCheckedOut)

Hi there, welcome to the forums.

For any future questions, please keep in mind that it makes it much easier for us to help you if you correctly format your code when posting to the forums and if you also post the error that you’re getting.

I’ve formatted your post correctly for you on this occasion. :slight_smile:


The error that I presume you’re getting, because it’s the one I get when I run your code, is this:

TypeError: Cannot set property _title of #<Media> which has only a getter

This is because you’ve made a few errors with the naming of your getter methods on the Media class.

Here’s the instruction for Step 4:
image

You’re meant to create getters for the property title, not _title. :slight_smile:

The class property remains this._title, but the leading underscore suggests that it’s a “private” property and so one which requires an explicit getter to provide the property in the required fashion. So, for example, you would need to declare the following:

get title() {
    return this._title;
}

Does that make sense?

Yes Thanks alot. This error got resolved. but one more error occured.

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

TypeError: historyOfEverything.toggleCheckoutStatus is not a function

Read the error message closely. Do you have a function with the name in question?

i am getting these errors after step 16.

/home/ccuser/workspace/learn-javascript-classes-build-a-library/app.js:16
set isCheckedOut(val){
^

RangeError: Maximum call stack size exceeded
at Book.set isCheckedOut [as isCheckedOut] (/home/ccuser/workspace/learn-javascript-classes-build-a-library/app.js:16:17)
at Book.set isCheckedOut [as isCheckedOut] (/home/ccuser/workspace/learn-javascript-classes-build-a-library/app.js:17:21)
at Book.set isCheckedOut [as isCheckedOut] (/home/ccuser/workspace/learn-javascript-classes-build-a-library/app.js:17:21)
at Book.set isCheckedOut [as isCheckedOut] (/home/ccuser/workspace/learn-javascript-classes-build-a-library/app.js:17:21)
at Book.set isCheckedOut [as isCheckedOut] (/home/ccuser/workspace/learn-javascript-classes-build-a-library/app.js:17:21)
at Book.set isCheckedOut [as isCheckedOut] (/home/ccuser/workspace/learn-javascript-classes-build-a-library/app.js:17:21)
at Book.set isCheckedOut [as isCheckedOut] (/home/ccuser/workspace/learn-javascript-classes-build-a-library/app.js:17:21)
at Book.set isCheckedOut [as isCheckedOut] (/home/ccuser/workspace/learn-javascript-classes-build-a-library/app.js:17:21)
at Book.set isCheckedOut [as isCheckedOut] (/home/ccuser/workspace/learn-javascript-classes-build-a-library/app.js:17:21)
at Book.set isCheckedOut [as isCheckedOut] (/home/ccuser/workspace/learn-javascript-classes-build-a-library/app.js:17:21)

The reason for that is a little trickier perhaps. What you have is a function that calls itself over and over.

You don’t want to use the setter from within the setter. You want to actually assign the value to the propery.

Hint:

Use the underscore to access the property from inside the setter.

1 Like