Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created May 29, 2020 02:59
Show Gist options
  • Save codecademydev/bd6b4aa43653cfb3b96b7c4f2e65e8f0 to your computer and use it in GitHub Desktop.
Save codecademydev/bd6b4aa43653cfb3b96b7c4f2e65e8f0 to your computer and use it in GitHub Desktop.
Codecademy export
// parent Class
class Media {
constructor(title) {
this._title = title;
this._isCheckOut = false;
this._ratings = [];
this._songs = {}
}
get title() {
return this._title;
}
get isCheckOut() {
return this._isCheckOut;
}
get ratings() {
return this._ratings;
}
get songs() {
return this._songs;
}
set isCheckOut(check) {
this.isCheckOut = check;
}
toggleCheckOutStatus() {
this._isCheckOut = !this._isCheckOut;
}
getAverageRating() {
const arrayRatings = this._ratings.length;
let ratingSum = this._ratings.reduce((currentSum, rating) => currentSum + rating, 0);
return ratingSum / arrayRatings;
}
addRating(newRating) {
if (Number.isInteger(newRating)) {
if (newRating >= 1 && 5 >= newRating) {
this._ratings.push(newRating);
}
} else {
console.log('There was an invalid rating submitted for ' + this._title);
}
}
addSong(newSong) {
this._songs.push(newSong);
}
};
// subclasses
// Book
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 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;
}
};
// CD
class CD extends Media {
constructor(title, author, songs) {
super(title);
this._author = author;
this._songs = [];
}
get author() {
return this._author;
}
get songs() {
return this.songs;
}
shuffle() {
}
}
// CD instance
console.log("***CD Instance***");
const cd = new CD('Ya Laila', 'Cheb Khaled', 'kifhalk lyom')
cd.toggleCheckOutStatus();
console.log(cd.isCheckOut);
cd.addRating(4)
cd.addRating(4)
cd.addRating(5)
console.log(cd.getAverageRating())
// Book instance
console.log("***Book Instance***")
const historyOfEverything = new Book('A Short History of Nearly', 'Bill Bryson', 544);
historyOfEverything.toggleCheckOutStatus();
console.log(historyOfEverything.isCheckOut);
historyOfEverything.addRating(4);
historyOfEverything.addRating(5);
historyOfEverything.addRating(5);
historyOfEverything.getAverageRating;
console.log(historyOfEverything.getAverageRating());
// Movie instance
console.log("***Movie Instance***")
const speed = new Movie('Jan de Bont', 'Speed', 116);
speed.toggleCheckOutStatus();
console.log(speed.isCheckOut);
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