-
-
Save codecademydev/ecbe6a9c94d8b217e9af8f0b512fdbe6 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(check) { | |
this._isCheckedOut = check; | |
} | |
toggleCheckOutStatus() { | |
this.isCheckedOut = !this.isCheckedOut; | |
} | |
getAverageRating() { | |
const total = this.ratings.reduce((acc, rating) => acc + rating); | |
const ratingLength = this.ratings.length(); | |
const average = total / ratingLength; | |
return average; | |
} | |
addRating(value) { | |
this.ratings.push(value); | |
} | |
} | |
class Book extends Media { | |
constructor(author, title, pages) { | |
super(title); | |
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 Bryson', '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()); | |
const speed = new Movie('Jan de Bont', 'Speed', 116); | |
speed.toggleCheckOutStatus; | |
console.log(speed.isCheckedOut); | |
speed.addRating(1); | |
speed.addRating(1); | |
speed.addRating(5); | |
console.log(speed.getAverageRating()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment