-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
374 lines (308 loc) · 10.2 KB
/
app.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
"use strict";
//The purpose of this Lab is to get a solid understanding of the .filter() and .map() advanced array methods.
//These methods will be used extensively on future projects
//Dataset
let dishes = [
{
"id": 1,
"name": "Pizza",
"cuisine": "Italian",
"servings": 8,
"ingredients": ["tomato", "cheese"]
},
{
"id": 2,
"name": "Spaghetti",
"cuisine": "Italian",
"servings": 2,
"ingredients": ["tomato", "cheese"]
},
{
"id": 3,
"name": "Ravioli",
"cuisine": "Italian",
"servings": 2,
"ingredients": ["tomato", "cheese"]
},
{
"id": 4,
"name": "Enchiladas",
"cuisine": "Mexican",
"servings": 6,
"ingredients": ["tomato", "cheese"]
},
{
"id": 5,
"name": "Tacos",
"cuisine": "Mexican",
"servings": 4,
"ingredients": ["tomato", "cheese"]
},
{
"id": 6,
"name": "Burrito Supreme",
"cuisine": "Mexican",
"servings": 1,
"ingredients": ["tomato", "cheese"]
},
{
"id": 7,
"name": "Elote",
"cuisine": "Mexican",
"servings": 4,
"ingredients": ["corn", "cheese"]
},
{
"id": 8,
"name": "Crepes",
"cuisine": "French",
"servings": 1,
"ingredients": ["flour", "sugar"]
},
{
"id": 9,
"name": "Corned Beef & Cabbage",
"cuisine": "Irish",
"servings": 10,
"ingredients": ["beef", "cabbage"]
},
{
"id": 10,
"name": "Beef Stew",
"cuisine": "Irish",
"servings": 8,
"ingredients": ["beef", "tomato"]
},
{
"id": 11,
"name": "Lasagna",
"cuisine": "Vegetarian",
"servings": 8,
"ingredients": ["tomato", "cheese"]
},
{
"id": 12,
"name": "Falafel",
"cuisine": "Vegetarian",
"servings": 1,
"ingredients": ["chickpea", "parsley"]
},
{
"id": 13,
"name": "Chili",
"cuisine": "Vegetarian",
"servings": 13,
"ingredients": ["tomato", "chickpea"]
},
{
"id": 14,
"name": "Goulash",
"cuisine": "Hungarian",
"servings": 15,
"ingredients": ["beef", "tomato"]
},
{
"id": 15,
"name": "Pho",
"cuisine": "Vietnamese",
"servings": 4,
"ingredients": ["beef", "ginger"]
},
]
//Example function
//IMPORTANT: Take the time to step through this example function with a breakpoint until you could explain what is going on to someone else before starting this lab.
function filterExample(){
//Debug tip: Use a console.log(el) inside the filter function to get a visualization of what el represents and see all its properties! This helps you to know what you can access with dot notation inside the filter. Do this every time you use a .filter or else you are working in the dark!
let results;
results = dishes.filter(function(el){
console.log("el inside filterExample's filter: ", el)
if(el.cuisine === "Mexican"){
return true;
}
else{
return false;
}})
return results;
}
let mexicanFood = filterExample();
console.log('mexicanFood from filterExample', mexicanFood)
//Reminder: Do not move on to problem one until understand the example completely!!
//1. Create a function that will return all dishes with the cuisine type of "vegetarian"
//Filter
function problemOne(){
let results;
results = dishes.filter(function (el) {
console.log("el inside filterExample's filter: ", el)
if (el.cuisine === "Vegetarian") {
return true;
}
else {
return false;
}
})
return results;
}
let vegetarianFood = problemOne();
console.log('vegetarian from filterExample', vegetarianFood)
//2. Create a function that will prompt the user to enter a cuisine type and then return all dishes that match that type
//Filter
function problemTwo() {
let results;
let choiceCuisine = prompt('Please choose what type of cuisine you would like: Vegetarian, Italian, French, Irish, Hungarian, Vietnamese, or Mexican');
results = dishes.filter(function (el) {
console.log("el inside filterExample's filter: ", el);
if (el.cuisine === choiceCuisine) {
return true;
}
else {
return false;
}
})
return results;
}
let chosenFood = problemTwo();
console.log(`User's choice of cuisine type from filterExample`, chosenFood)
//3. Create a function that will return all dishes with the cuisine type of "Italian" and a serving size greater than 5.
//Filter
function problemThree() {
let results;
results = dishes.filter(function (el) {
console.log("el inside filterExample's filter: ", el)
if (el.cuisine === "Italian" && el.servings > 5) {
return true;
}
else {
return false;
}
})
return results;
}
let italianServings = problemThree();
console.log(`Cuisine type of "Italian" and a serving size greater than 5.`, italianServings)
//4. Create a function that will return only dishes whose id number matches their serving count.
//Filter
function problemFour() {
let results;
results = dishes.filter(function (el) {
console.log("el inside filterExample's filter: ", el)
if (el.id === el.servings) {
return true;
}
else {
return false;
}
})
return results;
}
let idMatchesServings = problemFour();
console.log(`Dishes whose id number matches their serving count.`, idMatchesServings)
//5. Create a function that will return only dishes whose serving count is even.
//Filter
function problemFive() {
let results;
results = dishes.filter(function (el) {
console.log("el inside filterExample's filter: ", el)
if (el.servings % 2 == 0) {
return true;
}
else {
return false;
}
})
return results;
}
let evenServingCount = problemFive();
console.log(`Dishes whose serving count is even.`, evenServingCount)
//6. Create a function that will return dishes whose ingredients array INCLUDES "chickpea".
//Hint: You do not want to check the array's indexes to find out what the array INCLUDES.
//Double Hint: Research 'javascript does array include item' https://www.w3schools.com/Jsref/jsref_includes_array.asp#:~:text=JavaScript%20Array%20includes%20%28%29%201%20Definition%20and%20Usage,2016%29%20is%20supported%20in%20all%20modern%20browsers%3A%20
//Filter
function problemSix() {
let results;
results = dishes.filter(function (el) {
console.log("el inside filterExample's filter: ", el)
if (el.ingredients.includes("chickpea")) {
return true;
}
else {
return false;
}
})
return results;
}
let chickpeaDishes = problemSix();
console.log(`Dishes whose ingredients array INCLUDES "chickpea".`, chickpeaDishes)
//7. Create a function that will prompt the user to type the name of one ingredient. Then use a filter to find all the dishes whose ingredients array INCLUDES that ingredient. Return the new array.
//Filter
function problemSeven() {
let results;
let choiceIngredient = prompt('Please choose what ingredient you would like: Tomato, Chickpea, Cheese, Beef, Parsley, Cabbage, Flour, Sugar, Ginger, or Corn');
results = dishes.filter(function (el) {
console.log("el inside filterExample's filter: ", el)
if (el.ingredients.includes(choiceIngredient)) {
return true;
}
else {
return false;
}
})
return results;
}
let ingredientDishes = problemSeven();
console.log(`Dishes that include your choice ingredient.`, ingredientDishes)
//8a. Create a function that will return an array of the string cuisine types. Ie, ["Italian", "Italian", "Mexican", ...]
//Map
function problemEight() {
let results;
results = dishes.map(function (el) {
return el.cuisine
})
return results
}
let cuisineTypes = problemEight();
console.log(`Array of the string cuisine types.`, cuisineTypes)
//9. Create a function that will return an array of strings, with the cuisine type appended to the start of the dish's name. Ie, ["Italian Pizza", "Italian Spaghetti", ...]
//Map
function problemNine() {
let results;
results = dishes.map(function (el) {
return [el.cuisine, el.name].join(" ")
})
return results
}
let cuisineAndName = problemNine();
console.log(`Array of strings, with the cuisine type appended to the start of the dish's name.`, cuisineAndName)
//10. Create a function that will use advanced array methods on the 'dishes' array and return the result ["Vegetarian Lasagna", "Vegetarian Falafel", "Vegetarian Chili"]
function problemTen() {
let results;
let cuisine = "Vegetarian"
results = dishes.map(function (el) {
return cuisine +" " + el.name
})
return results
}
let vegetarianAndName = problemTen();
console.log(`Use advanced array methods on the 'dishes' array and return the result preceeded by "Vegetarian."`, vegetarianAndName)
//BONUS
//8b. Use the filter method to eliminate duplicate from problem 8a.
//11. Create a function that will return dishes whose ingredients array INCLUDES "tomato" OR "cheese".
//Hint: You do not want to check the array's indexes to find out what the array INCLUDES.
//Filter
function problemEleven() {
let results;
results = dishes.filter(function (el) {
console.log("el inside filterExample's filter: ", el)
if (el.ingredients.includes("tomato") || el.ingredients.includes("cheese")) {
return true;
}
else {
return false;
}
})
return results;
}
let tomatoCheese = problemEleven();
console.log(`Dishes whose ingredients array INCLUDES "tomato" OR "cheese."`, tomatoCheese)
//12. Create a function that will return the total serving count of all dishes.
//Must use Reduce, not a loop.
//13. Create a function that will return an array of any objects that do not share a cuisine type with any other objects.