-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-library.js
149 lines (123 loc) · 3.03 KB
/
build-library.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Create parent class for book, movie, cd
class Media{
constructor(title){
this._title = title;
this._isCheckedOut = 0;
this._ratings = [];
}
get title(){
return this._title;
}
get isCheckedOut(){
if(this._isCheckedOut === 1){
return `${this._title} is now available.`
} else {
return `${this._title} is not available now. `
}
}
get ratings(){
return this._ratings;
}
set isCheckedOut(param){
this._isCheckedOut = param;
}
toggleCheckOutStatus(){
this._isCheckedOut = !this._isCheckedOut;
}
getAverageRating(){
const sum = this.ratings.reduce((paramOne,paramTwo)=>{
return paramOne+paramTwo;
});
const num = sum/this.ratings.length;
return `The Average Rating : ${num.toFixed(2)}`;
}
addRating(newRating){
this.ratings.push(newRating);
}
}
// Create Book class
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;
}
}
// Create Movie class
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;
}
}
// Create CD class
class CD extends Media {
constructor(title, artist){
super(title);
this._artist = artist;
this._songs = [];
}
get artist(){
return this._artist;
}
get songs(){
return this._songs;
}
addSongs(newSong){
this.songs.push(newSong);
}
shuffle(){
let array = this.songs;
for(let i = array.length - 1; i > 0; i--){
const j = Math.floor(Math.random()*(i+1));
[array[i], array[j]] = [array[j],array[i]];
}
return array;
}
}
// Create Book instance
const historyOfEverything = new Book('A Short History of Nearly Everything', 'Bill Bryson', 544);
historyOfEverything.toggleCheckOutStatus();
console.log(historyOfEverything.isCheckedOut); // true
historyOfEverything.addRating(4);
historyOfEverything.addRating(5);
historyOfEverything.addRating(5);
console.log(historyOfEverything.getAverageRating());
// Create Movie instance
const speed = new Movie('Speed','Jan de Bont',116);
speed.toggleCheckOutStatus();
console.log(speed.isCheckedOut);
speed.addRating(1);
speed.addRating(1);
speed.addRating(5);
console.log(speed.getAverageRating());
// Create CD instance
const reputation = new CD('Reputation','Taylor Swift');
reputation.addSongs('...Ready For it?');
reputation.addSongs('End Game');
reputation.addSongs('Delicate');
reputation.addSongs('So It Goes..');
reputation.addSongs('Getaway Car');
reputation.toggleCheckOutStatus();
console.log(reputation.isCheckedOut);
reputation.addRating(5);
reputation.addRating(4);
reputation.addRating(5);
console.log(reputation.getAverageRating());
// Shuffle songs of reputation
console.log(reputation.songs);
let shuffleReputation = reputation.shuffle();
console.log(shuffleReputation);