Build a Library - Added Rating Parameters and Broke Code

Hello Codecademy Community,

I’m almost done with my Build a Library project, however, when I added in parameters for ratings to ensure the inputs are between 1 and 5, I broke my code. The error I’m receiving is for ‘class Book extends Media’ and it’s apparently a ‘SyntaxError: Unexpected identifier’.

I have a feeling the class Media extension code broke as my addRating() method has a problem. Can anyone help?

Thank you

Rohini

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(newIsCheckedOut) {
    this._isCheckedOut = newisCheckedOut;
  }
  
  toggleCheckOutStatus() {
    this._isCheckedOut = !this._isCheckedOut;
  }
  
  getAverageRating() {
    let ratingsSum =  this._ratings.reduce((currentSum, rating) => currentSum + rating, 0); 
    let lengthOfRatings = this._ratings.length;
    return ratingsSum / lengthOfRatings;
  }
  
  addRating(rating) {
    if (rating < 1 || rating > 5){
      console.log('Error! Please rate between 1 and 5')} else {
    this._ratings.push(rating);
    }
  }

class Book extends Media {
  constructor(title, author, pages) {
    super(title);
    this._author = author;
    this._pages = [];
  }
  
  get author() {
    return this._author;
  }
  
  get pages() {
    return this._pages;
  }
}

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('A Short History of Nearly Everything', 'Bill Bryson', 544);

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

const speed = new Movie('Speed', 'Jan De Bont', 116);
speed.toggleCheckOutStatus();
console.log(speed.isCheckedOut);
speed.addRating(1);
speed.addRating(1);
speed.addRating(5);
console.log(speed.getAverageRating());

//console.log(speed._title);
//console.log(speed._director);

Hello, @byteplayer10449.

Look closely at your code here:

You are starting a new class (Book), but have you closed the previous one (Media)?

1 Like

:woman_facepalming:t2: got it! I needed a curly bracket to close off my Media class.

Thank you!

1 Like

You’ve done a very nice job with indentation which made it really easy to spot the missing brace. My personal preference is to initially include both opening and closing braces, brackets, parenthesis, and then fill in the code between them, so I don’t forget to close them.

1 Like

That’s a good tip. Thank you so much, I really appreciate it. It can be so frustrating when you can’t figure out the one thing that is breaking your code!

1 Like

Most modern IDEs/code editors (like VSCode, Atom etc) do this automatically by default, much like how the CC learning environment does. :slight_smile:

But it extends to other language constructs - to always have a syntactically valid program after each edit.

Would be kinda silly to leave invalid code behind and move on to something else.

It’s also easier to get the syntax right for something minimal (empty) and then make additions to it, running a syntax checker on it after each addition (automatically, obviously)

And when everything else is valid, any error messages will be specifically about the thing currently worked on, which helps a lot as well.

I think codecademy has the unfortunate effect of giving off the impression that code is written all at once and … then it works? Idk. I get that feeling sometimes from questions asked here.
But that is probably done by copying text provided by codecademy and then running it and … then nothing was actually programmed, only copied.
And if there are problems with that code then there’s no way to approach it because big scary error messages and what not.
But the approach of making small edits and checking the program after each such edit gets around all that, makes it interactive.

But yeah, tools are a big part of facilitating that way of editing. Formatters (wrong things often come out looking odd when reformatted) and linters. All there is in codecademy’s web environment is make an edit and run it which is … reasonable and sufficient, but not obvious and not the most convenient.

1 Like