-
-
Save codecademydev/9dd97398afc219ef8c22833a0dfd8a34 to your computer and use it in GitHub Desktop.
Codecademy export
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(value){ | |
this._isCheckedOut = value; | |
} | |
toggleCheckOutStatus(){ | |
if(this._isCheckedOut === false){ | |
this._isCheckedOut = true; | |
}else { | |
this._isCheckedOut = false; | |
} | |
} | |
getAverageRating(){ | |
const avgRating = this.ratings.reduce((acc,curVal)=>{ | |
return Math.floor(acc + curVal / this.ratings.length); | |
}) | |
return avgRating; | |
} | |
addRating(num){ | |
this._ratings.push(num); | |
} | |
}; | |
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 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.ratings); | |
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.ratings) | |
console.log(speed.getAverageRating()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment