-
-
Save codecademydev/4469a236b2685a916078f82c1f220597 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
//Super Class - Common Elements: title(string), isCheckedOut(boolean, initially false), rattings(array, initially empty), getAverageRating(method), toggleCheckOutStatus(method), addRating(method) | |
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(checkOutStatus){ | |
this._isCheckedOut = checkOutStatus; | |
} | |
*/ | |
getAverageRating() { | |
let ratingsSum = this.ratings.reduce((accumulator, currentVal) => accumulator + currentVal, 0); | |
return ratingsSum / this.ratings.length; | |
} | |
toggleCheckOutStatus() { | |
this._isCheckedOut = !this._isCheckedOut // | |
} | |
addRatings(rating) { | |
this.ratings.push(rating); | |
} | |
} | |
//Book class with Media inheritance | |
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; | |
} | |
} | |
//Movie class with Media inheritance | |
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; | |
} | |
} | |
//CD class with Media inheritance | |
class CD extends Media { | |
constructor(title, artist, runTime) { | |
super(title); | |
this._artist = artist; | |
this._runTime = runTime; | |
} | |
get artist(){ | |
return this._artist; | |
} | |
get runTime(){ | |
return this._runTime; | |
} | |
} | |
//Test CD class with Media inheritance. | |
const CD1 = new CD('Man on The Moon', 'Kid Cudi', 30); //declare new CD object using classes and extension, save to CD1 variable. | |
CD1.toggleCheckOutStatus(); // bangs _isCheckedOut | |
console.log(CD1.isCheckedOut); // reads true | |
CD1.addRatings(5); // pushes 5 to Ratings array | |
CD1.addRatings(4); // pushes 4 to Ratings array | |
console.log(CD1.ratings); // reads [5, 4] | |
console.log(CD1.getAverageRating()); // reads 4.5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment