-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildALibrary.js
More file actions
237 lines (177 loc) · 7.75 KB
/
Copy pathBuildALibrary.js
File metadata and controls
237 lines (177 loc) · 7.75 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*Build a Library
Congratulations, you’ve become head librarian at your local Books-‘N-Stuff,
which is in dire need of your help. They’re still using index cards to organize
their content! Yikes.
But no matter, you know some JavaScript, so let’s get to work modernizing your new digs.
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. These three subclasses have the following properties and methods:
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()
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()
CD
Properties: _artist (string), _title (string),
_isCheckedOut (boolean, initially false), _ratings (array, initially empty),
and _songs (array of strings)
Getters: all properties have a getter
Methods: .getAverageRating(), .toggleCheckOutStatus(), and .addRating() */
class Media{
constructor(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(value){
this._isCheckedOut= value;
}
toggleCheckOutStatus(){
this._isCheckedOut = !this._isCheckedOut;
}
getAverageRating() {
const sum = this.ratings.reduce((accumulator, rating) => {
return accumulator + rating;
}, 0);
return sum / this.ratings.length;
}
addRating(ratingvalue){
if (ratingvalue >= 1 && ratingvalue <= 5) {
this._ratings.push(ratingvalue);
}
}
}
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);
// historyOfEverything.toggleCheckOutStatus();
// historyOfEverything.addRating(4);
// historyOfEverything.addRating(5);
// historyOfEverything.addRating(5);
// console.log(`History Book Rating => ${historyOfEverything.getAverageRating()}`);
// console.log(historyOfEverything);
// const speed = new Movie ('Jan de Bont', 'Speed', 116);
// speed.toggleCheckOutStatus();
// speed.addRating(1);
// speed.addRating(1);
// speed.addRating(5);
// console.log(`Average Rating for Speed => ${speed.getAverageRating()}`);
// console.log(`Average Rating for Speed => ${speed.getAverageRating()}`);
// console.log(speed);
/*1. Let’s start by making a parent class for our Book, CD, and Movie classes.
Create an empty class called Media.
2. Inside the Media class, create an empty constructor() method that takes one parameter.
This argument will set the one property that is in all three subclasses of Media,
and does not have a default value.
3.Inside the constructor(), set the values for Media properties that Book, CD, and
Movie share.
4.Create title(), isCheckedOut(), and ratings() getter methods.
Each getter should return the value saved to the corresponding property from the
previous step.
5.Create an isCheckedOut() setter that updates the _isCheckedOut property.
6.Under your getters, create a method called toggleCheckOutStatus() that changes the
value saved to the _isCheckedOut property.
If the current value is true, then change it to false. If the current value is false,
then change it to true.
7.Under .toggleCheckOutStatus(), create a method named getAverageRating(). Return the average value of the ratings array.
Use the reduce() method to find the sum of the _ratings array. Divide this sum by the length of the _ratings array, and return the result. Use the ratings getter to access the underlying _ratings array.
Take a look at the hint if you need help with the syntax for summing an array of numbers with .reduce().
8.Let’s add a method named addRating() that accepts one argument, then uses .push() and the ratings getter to add it to the end of the _ratings array.
9.Next, we’ll build a Book class that extends Media. If you feel comfortable building the Book class on your own, give it a shot. If not, use the steps below to help you along the way.
Whether you want to follow the steps or not, use the list of properties, getters, and methods as a reference.
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()
Create an empty Book class that extends Media.
10.
Inside the Book class, create a constructor() that accepts three arguments. These arguments are used to set properties that do not have default values.
11.
Call super() on the first line of the Book class constructor() method. Pass any arguments that the parent constructor uses.
12.
Use the remaining arguments to set the _author and _pages properties in Book.
13.
Since our Book class inherits Media class properties and getters, we only need to create two new getters in the Book class.
Add two new getters to the Book class. Each getter should return the value saved to its matching property.
14.
Let’s see if you can create an entire Movie class using only the property, getter, and method specifications below:
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()
Take a look at the Hint to see step-by-step instructions.
15.
Create a Book instance with the following properties:
Author: 'Bill Bryson'
Title: 'A Short History of Nearly Everything'
Pages: 544
Save the instance to a constant variable named historyOfEverything.
16.
Call .toggleCheckOutStatus() on the historyOfEverything instance.
17.
Log the value saved to the _isCheckedOut property in the historyOfEverything instance using the isCheckedOut getter.
18.
Call .addRating() three times on historyOfEverything with inputs of 4, 5, and 5.
19.
Call .getAverageRating() on historyOfEverything. Log the result to the console.
20.
Create a Movie instance with the following properties:
Director: 'Jan de Bont'
Title: 'Speed'
Runtime: 116
Save the instance to a constant variable named speed.
21.
Call .toggleCheckOutStatus() on the speed instance.
22.
Log the value saved to the isCheckedOut property in the speed instance.
23.
Call .addRating() three times on speed with inputs of 1, 1, and 5.
24.
Call .getAverageRating() on speed. Log the result to the console.
25.
If you would like to continue working on this project, we have listed some avenues to
build on your existing progress.
Add more properties to each class (movieCast, songTitles, etc.), and getters to access them.
Create a CD class that extends Media.
In .addRating(), make sure the input is between 1 and 5.
Create a method called shuffle for the CD class. The method returns a randomly sorted array of all the songs in the songs property.
Create a class called Catalog that holds all of the Media items in our library.
*/