Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created February 8, 2021 19:08
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/8b20f4abf39637f9d4986bbe8104841d to your computer and use it in GitHub Desktop.
Save codecademydev/8b20f4abf39637f9d4986bbe8104841d to your computer and use it in GitHub Desktop.
Codecademy export
class Catalog {
constructor() {
this._collection = [];
}
get collection() {
return this._collection;
}
set collection(mediaItems) {
this._collection.push(mediaItems);
}
}
class Media {
constructor(title) {
this._title = title;
this._isCheckedOut = false;
this._ratings = [];
}
get title() {
return this._title;
}
get isCheckedOut() {
return this._isCheckedOut;
}
set isCheckedOut(newCheckOutVal) {
this._isCheckedOut = newCheckOutVal;
}
get ratings() {
return this._ratings;
}
set ratings(NewRat) {
this._ratings = NewRat;
}
toggleCheckOutStatus() {
if (this.isCheckedOut === true) {
this.isCheckedOut = false;
} else {
this.isCheckedOut = true;
}
}
getAvarageRating() {
const ratingSum = this.ratings.reduce((accumulator, currentValue) => accumulator + currentValue);
const ratingsLength = this.ratings.length;
const avrgRating = ratingSum / ratingsLength;
return this.ratings = avrgRating;
}
addRating(rating) {
if (rating <= 5 && rating >= 1) {
this.ratings.push(rating);
} else {
return 'The rating has to be between 1 and 5';
}
}
}
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;
}
}
class CD extends Media {
constructor(artist, title, songs) {
super(title)
this._artist = artist;
this._songs = [songs];
}
get artist() {
return this._artist;
}
get songs() {
return this._songs;
}
set songs(song) {
this._songs = song;
}
addSongs(newSong) {
this.songs.push(newSong);
}
shuffle() {
for (let i = this.songs.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
const temp = this.songs[i];
this.songs[i] = this.songs[j];
this.songs[j] = temp;
}
return this.songs;
}
}
const historyOfEverything = new Book('Bill Bryson', 'A Short History of Nearly Everything', 544);
//console.log(historyOfEverything);
historyOfEverything.toggleCheckOutStatus()
//console.log(historyOfEverything.isCheckedOut);
historyOfEverything.addRating(4)
historyOfEverything.addRating(5)
historyOfEverything.addRating(5)
historyOfEverything.getAvarageRating();
//console.log(historyOfEverything.ratings);
const speed = new Movie('Jan de Bont', 'Speed', 116);
//console.log(speed);
speed.toggleCheckOutStatus();
//console.log(speed.isCheckedOut);
speed.addRating(1);
speed.addRating(1);
speed.addRating(5);
speed.getAvarageRating();
//console.log(speed.ratings);
const michaelJackson = new CD('Michael Jackson','Bad','Bad');
michaelJackson.addSongs('Speed Demon');
michaelJackson.addSongs('Liberian Girl');
// Actual array of songs
console.log(michaelJackson.songs);
//Shuffle array of songs
console.log(michaelJackson.shuffle());
michaelJackson.toggleCheckOutStatus();
//console.log(speed.isCheckedOut);
michaelJackson.addRating(3);
michaelJackson.addRating(4);
michaelJackson.addRating(5);
michaelJackson.getAvarageRating();
console.log(michaelJackson)
const itemsCatalog = new Catalog;
itemsCatalog.collection = historyOfEverything;
itemsCatalog.collection = speed;
itemsCatalog.collection = michaelJackson;
console.log(itemsCatalog);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment