Build a Library


#1

Hi there! I have a problem with my js-code. I know there is something wrong but I can’t find the fault. I hope someone can help me.

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 = ratingsSum / lengthOfArray;
    return average;
	}
}

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 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(title, artist, songs){
    super(title);
    this._artist = artist;
    this._songs = songs;
  }
  get artist(){
    return this._artist;
  }
  get songs(){
    return this._songs;
  }
  
}
  
 	Media.toggleCheckOutStatus();

	Media.addRating(4,5,5);
	console.log(Media.getAverageRating());
	const speed = new Movie("Speed",["jan de Bont","116"];
  speed. .toggleCheckOutStatus();

speed.addRating(1,1,5);
  speed.getAverageRating();

https://www.codecademy.com/courses/learn-javascript-classes/projects/build-a-library?course_redirect=introduction-to-javascript

Thank in advance!


#2

look at the parentheses on this line:

const speed = new Movie("Speed",["jan de Bont","116"];

not all parentheses have a matching closing parenthesis

here:

speed. .toggleCheckOutStatus();

why two full stops? full stops = .


#3

Hello stetim94,

thanks for your help! :slight_smile:
But there is still something wrong… :frowning:

best regards jaba2017


#4

can i see an updated version of your code? What do you think is wrong? You want my help, so please be a bit for coming, i am here to help you to improve your understanding and skill :slight_smile:


#5

hello stetim94,
thats my code now :

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 = ratingsSum / lengthOfArray;
    return average;
	}
}

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 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(title, artist, songs){
    super(title);
    this._artist = artist;
    this._songs = songs;
  }
  get artist(){
    return this._artist;
  }
  get songs(){
    return this._songs;
  }
  
}
  
 	Media.toggleCheckOutStatus();

	Media.addRating(4,5,5);
	console.log(Media.getAverageRating());
	const speed = new Movie("Speed",["jan de Bont","116"]);
  speed.toggleCheckOutStatus();

speed.addRating(1,1,5);
  speed.getAverageRating();

best regards jaba2017


#6

okay, that was 50% of my question. What do you think caused the error? What could possible be the problem?

Look, i can help you, but if you explain to me why you think things go wrong, i can see your thought process, correct any problems in your thought process, so you know better how to handle such a situation the next time. It will teach you so much more.


#7

step 20 of the instructions:

Call .toggleCheckOutStatus() on the speed instance.

Did you do this? to me, it seems you called this method on the class itself, not on the instance as instructed


#8

Hello stetim94,
that was very helpful --> THANK YOU SO MOUCH :smiley:
I found some other faults ^^
That’s the code which workers :

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._author = author;
this._pages = 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;
}

}

class cd extends Media{
constructor(title, artist, songs){
super(title);
this._artist = artist;
this._songs = songs;
}
get artist(){
return this._artist;
}
get songs(){
return this._songs;
}

}

const speed = new movie(“Speed”,“jan de Bont”,116);
console.log(speed);
speed.toggleCheckOutStatus();
console.log(speed);
speed.addRating(1);
speed.addRating(1);
speed.addRating(5);
//console.log(speed);
speed.getAverageRating();
console.log(speed);


#9