Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created May 3, 2020 15:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codecademydev/543230371716f4ffdc457d1487fdc1b7 to your computer and use it in GitHub Desktop.
Save codecademydev/543230371716f4ffdc457d1487fdc1b7 to your computer and use it in GitHub Desktop.
Codecademy export
class Media {
constructor(title) {
this._title = title ;
this._isCheckedOut = false;
this._ratings = [];
}
get title() {
return this._title;
}
get ratings () {
return this._ratings;
}
get isCheckedOut() {
return this._isCheckedOut;
}
toggleCheckOutStatus(){
if(this.isCheckedOut === true){
this.isCheckedOut = false;
} else {
this.isCheckedOut = true;
}
}
getAverageRating() {
let ratingsSum = this.ratings.reduce((currentSum,rating) => currentSum + rating);
const lengthOfArray = this.ratings.length;
let result = ratingsSum / lengthOfArray;
return result;
}
addRating(userRating){
this.Ratings.push(userRating);
}
set isCheckedOut(newKey){
this._isCheckedOut = newKey;
}
}
class Book extends Media {
constructor(author,title,pages){
super(title);
super(isCheckedOut);
super(ratings);
this._author = author;
this._pages = pages;
}
get author() {
return this._author;
}
get pages() {
return this._pages;
}
}
class Movie extends Media {
constructor(director,title,runTime) {
super(title);
this._director = director;
this._runTime = runTime;
}
get director () {
return this._director;
}
get runTime() {
return this._runTime;
}
}
const historyOfEverything = new Book('Bill Brayson','A Short History of Nearly Everything',544)
historyOfEverything.toggleCheckOutStatus()
console.log(historyOfEverything.isCheckedOut)
historyOfEverything.addRating(4)
historyOfEverything.addRating(5)
historyOfEverything.addRating(5)
console.log(historyOfEverything.getAverageRating())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment