Skip to content

Instantly share code, notes, and snippets.

@stefancoding7
Last active June 1, 2024 06:49
Show Gist options
  • Save stefancoding7/61d0d68d9ae2a3db0d45a047b15c50a9 to your computer and use it in GitHub Desktop.
Save stefancoding7/61d0d68d9ae2a3db0d45a047b15c50a9 to your computer and use it in GitHub Desktop.
Build a library-Codecademy solution(Javascript)
class Media {
constructor(title) {
this._title = title;
this._ratings = [];
this._isCheckedOut = false;
}
// getters for title, isCheckedOut and ratings
get title() {
return this._title;
}
get isCheckedOut() {
return this._isCheckedOut;
}
get ratings() {
return this._ratings;
}
toggleCheckOutStatus() {
this._isCheckedOut = !this._isCheckedOut;
}
addRating(inputValue) {
if(inputValue <= 5) {
this._ratings.push(inputValue);
} else {
console.log('Rating have to be under 5');
}
}
// sum all rating numbers in array and divide it with the length of array
getAverageRating() {
let sum = this._ratings.reduce((accumulator, rating) => accumulator + rating, 0);
return Math.floor(sum / this._ratings.length);
}
set isCheckedOut(checkIn) {
this._isCheckedOut = checkIn;
}
}
// book class
class Book extends Media {
constructor(author, title, pages, genre ) {
super(title);
this._author = author;
this._pages = pages;
this._genre = genre;
}
get author() {
return this._author;
}
get pages() {
return this._pages;
}
}
// movie class
class Movie extends Media {
constructor(director, title, runTime, movieCast) {
super(title);
this._director = director;
this._runTime = runTime;
this._movieCast = movieCast;
}
get director() {
return this._director;
}
get runTime() {
return this._director;
}
get movieCast() {
return this._movieCast;
}
}
// cd class
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;
}
// get the randomly sorted array of all the songs in the songs property
shuffle() {
return this._songs.sort(() => Math.random() - 0.5);
}
}
// ---------------------------------------------------start Book ----------------------------------
// instance Book
const historyOfEverything = new Book('Bill Bryson', 'A Short History of Nearly Everything', 544);
historyOfEverything.toggleCheckOutStatus();
console.log(historyOfEverything.isCheckedOut);
// add ratings
historyOfEverything.addRating(4);
historyOfEverything.addRating(5);
historyOfEverything.addRating(5);
console.log(historyOfEverything.getAverageRating());
// ---------------------------------------------- Start Movie ---------------------------------------------
const speed = new Movie('Jan de Bont', 'Speed', 116, 'Chandler Bing');
speed.toggleCheckOutStatus();
console.log(speed.isCheckedOut);
speed.addRating(1);
speed.addRating(1);
speed.addRating(5);
console.log(speed.getAverageRating());
//------------------------------------------------Start Cd ----------------------------------------------------
const stateOfTrance = new CD('Armin Van buuren', 'State of Trance', ['first song', 'second song', 'theerd song', 'Another song', 'Last song']);
stateOfTrance.toggleCheckOutStatus();
console.log(stateOfTrance.isCheckedOut);
stateOfTrance.addRating(4);
stateOfTrance.addRating(5);
stateOfTrance.addRating(5);
// return shuffled songs
console.log(stateOfTrance.shuffle());
@AntonioHalvorsen
Copy link

AntonioHalvorsen commented Apr 11, 2022

Thank you, that helped me a lot. I am now working on a project with my students to improve the quality of education. If you are a student at the moment, it is worth - here are free samples of ecse for students so that you can make your own sample assignment and learn the material better. This improves the question view the page now here are free samples of ecse for students so that you can make your own sample assignment and learn the material better. This improves the quality of education and learning. It's a departure from the classic educational system, but it also improves the quality of knowledge and saves a lot of time for learning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment