-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepExercises.js
336 lines (314 loc) · 13.6 KB
/
prepExercises.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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// /\/\/\/\/\/\/\/\/\/\ Exercises /\/\/\/\/\/\/\/\/\/\
// ~~~~~~~~~~~~~~~~~~~~~~ Basic Requirments ~~~~~~~~~~~~~~~~~~~~~~
// 0.Some Terms to know:
// Data: Strings, booleans, numbers, arrays and objects.
// Model: A way of representing something as data.
// Instance: A single representation of something as using a model. For our purposes, this will be an object.
// Factory function: A function that outputs instances of a model.
// 1. Think about some different attributes of books – what do all books have? Ideas include:
//
// Title
// Author
// MSRP
// Genre
// Number of Pages
// Description
// 2.In terms of the properties of books that you thought of, represent the following books as data:
// Harry Potter and the Sorcerer's Stone (J.K. Rowling)
// Romeo and Juliet (William Shakespeare)
// Structure and Interpretation of Computer Programs (Gerald Jay Sussman, Hal Abelson)
// NOTE: Did you account for the possibility of two authors? If not, update your model to handle multiple authors.
// Three other books (see this list for ideas)
var book1 = {
name: "Harry Potter and the Sorcerer's Stone",
author: "J.K. Rowling",
genre: "Fantasy",
"number of pages": 223,
price: "$11.99"
}
var book2 = {
name: "Romeo and Juliet",
author: "William Shakespeare",
genre: "Shakespearean tragedy",
"number of pages": "480",
price: "$14.99"
}
var book3 = {
name: "Structure and Interpretation of Computer Programs",
author: {author1: "Gerald Jay Sussman", author2: "Hal Abelson"},
genre: "Textbook",
"number of pages": 657,
price: "$54"
}
// 3.You may have been rewriting the same type of object over and over. We need to stay DRY (Do Not Repeat).
//Write a function makeBook that takes as arguments different attributes of a book and returns an object representing that book that has the proper structure
//(we call this a factory function).
function makeBook(name, auth1, auth2, genre, nofPages, price) {
var auth;
if (auth1 === " ")
auth = auth2;
else if (auth2 === " ")
auth = auth1;
else auth = {author1: auth1, author2: auth2};
return {
name: name,
author: auth,
genre: genre,
"number of pages": nofPages,
price: price
}
}
// 4.Look at one of your book objects in the console. This is the object inspector. The object inspector is nice to have,
// but it will be easier to have a function to display the more important information easily.
// Write a function called displayBook that takes a book as an argument, and returns the important information in a more readable way, for example:
// var sorcerersStone = { /* ... */ }
// function displayBook(book) {
// // ...
// }
// displayBook(sorcerersStone);
// // => 'Harry Potter and the Sorcerer's Stone, by J.K. Rowling -- fantasy, $24.99'
// The output string above is only an example. What information is most important to you? How can you make that information easier to read for people?
function displayBook(book){
let author;
if (typeof book.author === "object")
author = book.author.author1 + " and " + book.author.author1;
else author = book.author;
return book.name + ", " + "by " + author + ' --' + book.genre + ", " + book.price + '.'
}
// 5.Create an array called books that holds all of the books that you created above.
books = [book1, book2, book3]
// 6.Your function displayBook can be used to display a single book as a string. Now, write a function displayBooks that, given an array of books,
// returns a single string consisting of all of the books. Use the function displayBook to format all of the books.
//Each book should be numbered and separated with a newline (we also call this a line break) character so that each book is shown on a separate line in
//the console. The newline character is specified with a special escaped character in a string:
// // Enter the below line into a console 'Hello /n World!'; // the 'backslash n' character is a newline
// function displayBooks(books) {
// // ...
// }
// displayBooks(books);
// // => '1. Harry Potter and the Sorcerer's Stone... /n 2. Snow Crash, ...'
function displayBooks(array){
let author;
let result = "";
for(let i = 0; i < array.length; i++)
result += displayBook(array[i]) + "\n";
return result;
}
// 7.Write a function searchBooks that, given a query and an array of books, searches the array of books for 'matching' books.
//You will decide what way you want to write your search algorithm.
//Here are some things to think about: What fields will be searched? Will you search multiple fields simultaneously
//(it might be best to start with one field, e.g.title)?
//Should the search be case-sensitive? How will the search work? Will it only work from the beginning of a field, or from anywhere within? some hints:
// 'Harry Potter'.toLowerCase(); // => 'harry potter'
// 'Harry Potter'.substr(0, 5); // => 'Harry'
// var query = 'Harry';
// 'Harry Potter'.substr(0, query.length); // => 'Harry'
// 'Harry Potter'.indexOf('Pot'); // => 6
// 'Harry Potter'.indexOf('dog'); // => -1
// A good starting point would be to write a function isMatch that accepts two arguments – the query and a single book –
// and returns true if the book is a match, and false otherwise.
function isMatchb(books, name) { // this function searches for any query, that matches the name of book, author, genre
let values; // It could be a part of the name like pot or part of the author name like Jay
if (typeof name === "string") {
name = name.toLowerCase();
for (let j = 0; j < books.length; j++) {
values = Object.values(books[j]);
for (let i = 0; i < values.length; i++) {
if (typeof values[i] === "object") {
values[i] = Object.values(values[i]);
for (let n = 0; n < values[j][i].length; n++) {
if (values[i][n].toLowerCase().indexOf(name) !== -1)
return true;
}
}
else if (typeof values[i] === "string") {
if (values[i].toLowerCase().indexOf(name) !== -1)
return true;
}
}
}
}
return false
}
// 8.Write a function removeBook that, given a book's title and an array of books, returns a new array of books that does not contain the book with
//the provided title.
function removeBook(array, book) {
let check = 0;
for (let i = 0; i < array.length; i++) {
if (array[i].name.toLowerCase() === book.toLowerCase()) {
array.splice(i,1);
check++;
}
}
if(check === 0)
return "Name doesn't exist";
return array;
}
// ~~~~~~~~~~~~~~~~~~~~~~ More Practice ~~~~~~~~~~~~~~~~~~~~~~
// 1.As we did before, think about what kinds of aspects of movies you would like to represent.
//A few ideas are: Title ,Director ,Duration ,Release Date ,Actors/Actresses ,Studio(s) ,Synopsis ,Rating
// You can make this as detailed as you want. You also need to decide how you will store or present your data.
// For example, you can use an array to represent the actors/actresses.
// But if you want to include their roles, maybe you want to use something else. Did he/she win any awards?
// Even the rating of a movie is open to interpretation –
// is the rating from critics? Rotten Tomatoes (a famous American website that rates how good movies are)? Some combination?
var movie1 = {
title: "Mirage",
genre: ["Drama", "Mystery", "Romance" ],
"release date": 2018,
rating: 0.75,
cast: {actor1: {name: "Mario Casas", role: "Vera Roy" }, actor2: {name: "Bárbara Lennie", role: "Inspector Leyra"}},
duration: 128
}
var movie2 = {
title: "The invisible guest",
genre: ["Crime", "Mystery", "Thriller" ],
"release date": 2016,
rating: 0.67,
cast: {actor1: {name: "Mario Casas", role: "Adrián Doria, successful businessman, husband and father"},
actor2: {name: "Bárbara Lennie", role: "Laura Vidal, Adrián's lover" }},
duration: 106
}
var movie3 = {
title: "The platform",
genre: ["Horror", "Sci-Fi", "Thriller"],
"release date": 2019,
rating: 0.83,
cast: {actor1 : {name:"Iván Massagué", role: "Goreng"},
actor2: { name: "Zorion Eguileor", role: "Trimagasi" }},
duration: 94
}
// 2.Make five more movie objects using the same format you decided upon.
// 3. Write a factory function for movies. HINT: What is a factory function? We explained it above!
function makeMovie (title, [g1, g2, g3], releasedate, rating, [name1, role1, name2, role2],duration) {
return {
title: title,
genre: [g1, g2, g3],
"release date": releasedate,
rating: rating,
cast: {actor1 : {name: name1, role: role1}, actor2: { name: name2, role: role2 }},
duration: duration
}
}
// 4.Write a function displayMovie that works like displayBook, but for movies.
function displayMovie(movie){
return movie.title + " - " + movie["release date"] + ", Genre: " + movie.genre[0] + "."
}
// 5.Write a function displayCast that displays the cast of a movie, including: Role , Actor/Actress name
function displayCast(movie){
return movie.cast.actor1.name + " as " + movie.cast.actor1.role + " and " +
movie.cast.actor2.name + " as " + movie.cast.actor2.role + ".";
}
// 6.Create an array to hold the movies that you created called movies, and add your movies to it.
movies = [movie1, movie2, movie3];
// 7.As before, write a displayMovies function that works just like displayBooks.
function displayMovies(array){
let result = "";
for(let i = 0; i < array.length; i++) {
result += displayMovie(array[i]) + '\n'}
return result
}
// 8. Calculate the average length of your movies by writing a function called averageLength that will accept an array of movies as a parameter and
// output the average length. The difficulty of this problem is dependent on how you have chosen to store the duration.
function avgDuration(array){
let result = 0;
for(let i = 0; i < array.length; i++)
result += array[i].duration
let avg = result / array.length
return "Average duration for " + array.length + " movies is " + avg.toFixed(2) + " minutes.";
}
// How about averageRating?
function avgRating(array){
let result = 0;
for(let i = 0; i < array.length; i++)
result += array[i].rating
let avg = result / array.length
return "Average rating for " + array.length + " movies is " + 100 * avg.toFixed(2) + "%.";
}
// 9.How about searching your movies array? Write a function that works like searchBooks, but for movies.
function isMatchm(movies, name) { // this function searches for any query [genre or movie title], that matches the title or movie genre
let values; // if searching for name query It could be a part of the name while if searching for genre it must matches the whole genre.
if (typeof name === "string") {
name = name.toLowerCase();
for (let j = 0; j < movies.length; j++) {
values = Object.values(movies[j]);
for (let i = 0; i < values.length; i++) {
if (Array.isArray(values[i])) {
for (let n = 0; n < values[i].length; n++) {
if (values[i][n].toLowerCase() === name.toLowerCase())
return true;
}
}
else if (typeof values[i] === "string") {
if (values[i].toLowerCase().indexOf(name) !== -1)
return true;
}
}
}
}
return false
}
// ~~~~~~~~~~~~~~~~~~~~~~ Advanced ~~~~~~~~~~~~~~~~~~~~~~
// 1.Tagging System: Some books have multiple categories, have won awards, are on a best-seller list,
//or have other special characteristics. Let's incorporate a tagging system that
// will allow us to represent all of these. Write functions addTag and removeTag that each accept a book
// and a tag as parameters, and either add tags or remove tags respectively.
// Considerations:
// If you included a genre key, replace it with a tag.
// What if you use addTag on a book that has no tags yet?
// What if you attempt to use addTag with the same tag (on the same book) multiple times?
//Should it be possible to have the same tag twice?
// Add some tags to multiple books, like 'bestseller' or 'pulitzer'.
// Extend
// Extend searchBooks to work with tags.
function addTag(book, tag) {
if(searchBooks(books, tag))
return "tag already exist or wrong tag format";
let keys = Object.keys(book);
for (let i = 0; i < keys.length; i++) {
if (keys[i] === "genre") {
delete book[keys[i]];
book.tag = [];
book.tag.push(tag);
return book;
}
else if (keys[i] === 'tag') {
book.tag.push(tag);
return book;
}
}
book.tag = [];
book.tag.push(tag);
return book;
}
function searchBooks(books, name) { // this function searches for tag, a srting or array, number tages are wrong format.
let values;
if (typeof name === "string") {
name = name.toLowerCase();
for (let j = 0; j < books.length; j++) {
values = Object.values(books[j]);
for (let i = 0; i < values.length; i++) {
if (Array.isArray(values[i])) {
for (let n = 0; n < values[i].length; n++) {
if (values[i][n] === name)
return true;
}
}
else if(typeof values[i] === "string")
if(values[i] === name)
return true;
}
}
}
else if (typeof name === "number")
return true;
return false
}
// 2.Let's revisit your removeBooks function: what would happen if you had two books with the same title, but different authors?
// Would your algorithm remove both books? This is a common problem that we usually solve by providing a unique identifier for each item.
// Modify all of your books to contain an id key with a unique value. This can be an integer or a unique string (like an ISBN).
// Change removeBook to use the book's id for lookups instead of its title.
// 3.Can you think of a way to write a more abstract displayItem function that works for books and movies
//(depending on how you have structured your objects, this may or may not work well)?
// 4.Write a more general searchItems function that accepts as parameters the query, items to search, and an array of keys that should be searched.
//Refactor searchMovies and searchBooks to use this function.