Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created April 16, 2019 08:24
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/24666d769856cb61ecfb9968913bfea4 to your computer and use it in GitHub Desktop.
Save codecademydev/24666d769856cb61ecfb9968913bfea4 to your computer and use it in GitHub Desktop.
Codecademy export
class Media {
constructor(title) {
this._title = title;
this._isCheckedOut = false;
this._rating = [];
}
get title() {
return this._title;
}
get isCheckedOut() {
return this._isCheckedOut;
}
get rating() {
return this._rating;
}
set isCheckedOut(checkOut) {
this._isCheckedOut = checkOut;
}
toggleCheckOutStatus() {
this.isCheckedOut = !this.isCheckedOut
}
getAverageRating() {
let sum = this.rating.reduce((currentSum, rating) => currentSum + rating, 0);
return sum/this.rating.length;
}
addRating(newRating) {
return this.rating.push(newRating);
}
}
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;
}
}
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.getAverageRating());
const speed = new Movie('Speed', 'Jan de Bont', 116);
speed.toggleCheckOutStatus();
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