Created
April 19, 2023 21:14
-
-
Save Nalini1998/e8968eaed08ef85a4ae74cd997ae6fc9 to your computer and use it in GitHub Desktop.
Build a Library Project: Books-‘N-Stuff carries three different types of media: books, CDs, and movies. In this project you will create a parent class named Media with three subclasses: Book, Movie, and CD.
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
// Created by Nalini Vo from Vietnam | |
// Requires: | |
**1. Book** | |
**Properties**: Author (string), title (string), pages (number), isCheckedOut (boolean, initially false), and ratings (array, initially empty). | |
**Getters**: All properties have a getter | |
**Methods**: .getAverageRating(), .toggleCheckOutStatus(), and .addRating() | |
**2. Movie** | |
**Properties**: Director (string), title (string), runTime (number), isCheckedOut (boolean, initially false), and ratings (array, initially empty) | |
**Getters**: All properties have a getter | |
**Methods**: .getAverageRating(), .toggleCheckOutStatus(), and .addRating() | |
**3. CD** | |
**Properties**: Artist (string), title (string), isCheckedOut (boolean, initially false), and ratings (array, initially empty), songs (array of strings) | |
**Getters**: All properties have a getter | |
**Methods**: .getAverageRating(), .toggleCheckOutStatus(), and .addRating() | |
// My Code as below: | |
class Media { | |
contructor(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(newisCheckedOut) { | |
this._isCheckedOut = newisCheckedOut; | |
} | |
toggleCheckOutStatus () { | |
this._isCheckedOut = !this._isCheckedOut; | |
} | |
} | |
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); | |
console.log(historyOfEverything); | |
historyOfEverything.toggleCheckOutStatus(); | |
console.log(historyOfEverything.isCheckedOut); | |
historyOfEverything.addRating = [4,5,5]; | |
let numbers = historyOfEverything.addRating; | |
function getAverageRating(numbers) { | |
return numbers.reduce((x,y) => x+y)/numbers.length | |
} | |
console.log(getAverageRating(numbers)); | |
const speed = new Movie('Jan de Bont', 'Speed', 166); | |
console.log(speed); | |
speed.toggleCheckOutStatus(); | |
console.log(speed.isCheckedOut); | |
speed.addRating = [1, 1, 5]; | |
let speeds = speed.addRating; | |
function getAverageRating(speeds) { | |
return speeds.reduce((x,y) => x+y)/speeds.length | |
} | |
console.log(getAverageRating(speeds)); | |
class CD extends Media { | |
constructor(artist, title) { | |
super(title); | |
this._artist = artist; | |
} | |
get artist() { | |
return this._artist; | |
} | |
} | |
const shuffle = new CD('Nalini cutie', 'Minh bat chuoc loai Meow keu nha'); | |
console.log(shuffle); | |
shuffle.toggleCheckOutStatus(); | |
console.log(shuffle.isCheckedOut); | |
shuffle.addRating = [5,5,5]; | |
let shuffles = shuffle.addRating; | |
function getAverageRating(shuffles) { | |
return shuffles.reduce((x,y) => x+y)/shuffles.length | |
} | |
console.log(getAverageRating(shuffles)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://www.codecademy.com/workspaces/643de19d14c65d59652fc19c