-
Notifications
You must be signed in to change notification settings - Fork 3
/
exercises.js
502 lines (336 loc) · 13.7 KB
/
exercises.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
import { fileURLToPath } from "url";
if (process.argv[1] === fileURLToPath(import.meta.url)) {
/*
To run the code you write for each exercise, change the `exercise_01()`
code below to match the EXACT name
of the exercise, as it is written in the line `function exercise_xx`.
For Example:
If I want to run exercise_05 below,
I would change the code below from "exercise_01()" to
"exercise_05()", save this file.
Then, when I run this file by running `node exercise.js`
in the VS Code terminal while inside this folder, your code
for exercise_05 will run.
*/
// Modify the line of code BELOW to run a different exercise
exercise_01();
// Modify the line of code ABOVE to run a different exercise
}
function exercise_01() {
/*
Exercise 1
Arrays:
1. Create a variable assigned to an array of five numbers and
log the array to the console.
2. Then, Log the length of this array.
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line and code in this space";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_02() {
/*
Exercise 2
Access Array Elements:
1. Create a variable assigned to an array of five numbers.
2. Log the first and last elements of the numbers array to the console.
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line and code in this space";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_03() {
/*
Exercise 3
Add to Array:
Create a variable assigned to an empty array.
Use push() to add a number to the end of the array five times.
Then, log the array
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line and code in this space";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_04() {
/*
Exercise 4
Add to Beginning:
Create an array of 5 strings.
Use unshift() to add a number to the beginning of the array.
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line and code in this space";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_05() {
/*
Exercise 5
Remove from Array:
Create a variable assigned to an array of 5 strings.
Use pop() to remove the last item and log it to the console
Then log the array
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line and code in this space";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_06() {
/*
Exercise 6
Remove from Beginning:
Create a variable assigned to an array of 5 numbers.
Use shift() to remove the first item and log it to the console.
Then, log the array to the console.
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line and code in this space";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_07() {
/*
Exercise 7
Create a variable assigned an array containing the numbers from 1 to 10.
Check if the number 7 is in the array.
If it is log the message "Item is in the array!" to the console
Otherwise log the message "Item not present in the array".
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line and code in this space";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_08() {
/*
Exercise 8
Array Index:
Copy the array from exercise_08 below.
Use indexOf() to find the position of 4 in the array.
Then, using bracket notation on the `numbers` array,
print the element before and the element after 4 in the array
WARNING: Don't just manually type out and print 3 and 5 to the console.
Use bracket notation on the array to select the element before
and the element after the indexOf 4 in the array, and print THOSE to the
console.
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line and code in this space";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_09() {
/*
Exercise 9
Splice Array:
Copy the array from exercise_08 and paste it below.
Use splice() to remove the second item in the array.
Then print the array to the console
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line and code in this space";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_10() {
/*
Exercise 10
Maps:
Create a new Map, and set values for these 3 keys:
- name
- age
- job
Then log this Map to the console
Then create an object literal with these property/value pairs
and log it to the console.
Notice the difference on how it appears when logged to the console.
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line and code in this space";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_11() {
/*
Exercise 11
Get from Map:
Copy the Map creation code from exercise_10 and paste it below
Use the .get() method on the Map to retrieve the value for the "name" key.
Then, insert it into a string literal with the message:
`This person's name is [nameValue]`
Then log this to the console
*/
// CODE IN THE OPEN LINES BELOW
/*
Answer info:
This is how you instantiate a new map with values
Notice, you pass it an array of multiple sub-arrays, each containing two
elements each:
- The first element the name of the key
- The second element, the value of that key
*/
const placeholder = "Delete this line and code in this space";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_12() {
/*
Exercise 12
Delete from Map:
Copy the Map creation and key-value pair setting code from exercise_11 and
paste it below
Log the Map and it's size to the console
Then, use the Map's delete method to delete the entry with the key "age"
Log the Map and it's size to the console again
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line and code in this space";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_13() {
/*
Exercise 13
Sets:
Create a new Set with three unique values
Log the Set and its size to the console
Then add 3 more unique items to the Set
Log the Set and its size to the console
Then, add an item to the Set that is already in it.
Log the Set and its size to the console and notice that it
didn't add the duplicate
Then use the .has() method to check if the set has 3 separate items
Write a condition to store if the Set has all 3 items.
Then write an if-else statement that prints "Has all three" if it does and
"Does not contain all three" if it doesn't.
Finally, delete three items from the Set and log the Set and it's size to
the console.
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line and code in this space";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_14() {
/*
Exercise 14
While Loop:
Use a while loop and the increment operator to log numbers from 1 to 5.
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line and code in this space";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_15() {
/*
Exercise 15
Do-While Loop:
Use a do-while loop and the decrement operator to log numbers from 28 to 1.
Then add a conditional inside the loop to only print the number if it is
divisible by 7 OR 4
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line and code in this space";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_16() {
/*
Exercise 16
Break and Continue:
Create a for loop that iterates from 1 to 10 and logs
each number to the console.
Then, using the `continue` and `break` keywords, skip the loop iteration
each time the number is odd, and then break the loop once it reaches 8.
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line and code in this space";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_17() {
// DONT edit the code below
const checkoutItems = [
{ item: "toilet paper", price: 17.99, type: "toiletry" },
{ item: "1lb ground beef", price: 6.99, type: "food" },
{ item: "Tooth brush 3 pack", price: 7.99, type: "toiletry" },
{ item: "Iphone Charging Cable", price: 12.49, type: "electronics" },
{ item: "Chips", price: 3.49, type: "food" },
{ item: "Xbox Gift Card", price: 25, type: "gift card" },
{ item: "12 pack Diet Coke", price: 9.99, type: "food" },
{ item: "Visa Gift Card", price: 160, type: "gift card" },
{ item: "Vizio 50 inch tv", price: 299, type: "electronics" },
{ item: "3lb bag apples", price: 8.99, type: "gift card" },
{ item: "a banana", price: 20, type: "food" },
{ item: "5lb protein powder", price: 35, type: "food" },
{ item: "Dumbbell Set", price: 80, type: "fitness equipment" },
{ item: "10lb bag rice", price: 18.25, type: "food" },
{ item: "gallon vitamin d milk", price: 3.49, type: "food" },
{ item: "12pk eggs", price: 7.99, type: "food" },
{ item: "5pk Uncrustables", price: 10.99, type: "food" },
{ item: "5 bags steamable brocolli", price: 7, type: "food" },
];
// DONT edit the code above
/*
Exercise 17
Real World Application of For Loops:
We have defined an array above named `checkoutItems`.
It contains objects that report on different properties of items bought
by a customer at a supermarket.
For Loop Exercise pt. 1:
Purpose: We want to see all the names of the items in our cart in an easy
to read format. We want to automate this in case our checkout
cart becomes very large, making the time to manually write this
information out a burden.
1. In the space below, create a variable named `itemNames` and assign an empty
array to it.
2. Then, using a basic for loop, iterate through all elements in the
`checkoutItems` array and add the `item` property value of each object to
the `itemNames` array.
3. Then, below this basic for loop,
write a for-of loop that logs each of the itemNames to the console.
Before and after the for-of loop, write a console.log to give this
information a header (like "Items in Checkout Cart") and a footer ("--------")
to make the information easier to read in the console.
For Loop Exercise pt. 2:
Purpose: We want to see all the unique types of items we purchased
in an easy to read format.
1. Create a new variable named `itemTypes` that stores a new empty Set.
Place this variable next to the `itemNames` variable declaration from
pt. 1 of this exercise.
2. Then, add a new line within the basic for loop from pt. 1 that adds the `type`
property value of each object to the `itemTypes` set.
3. Then, write a for-of loop after the for-of loop from pt. 1
that logs each of the unique types from the cart to the console.
Before and after the for-of loop, write a console.log to give this
information a header (like "Unique Item Types") and a footer ("--------")
to make the information easier to read in the console.
For Loop Exercise pt. 3
Purpose: We want to see how much money we spent per unique item type.
We want to automate this task so we can keep an accurate track of
our areas of personal spending.
1. Create a new variable named `sumPricePerType` and assign it an empty
object literal.
Place this variable next to the others from pt.1 and pt. 2
2. Add a new line within the basic for loop from pt. 1
that grabs the current elements 'price'.
3. Then, see if the object `sumPricePerType` has a 'type' property that matches
the current element's type.
If it does, grab it's value and add the current price to it. Then, set the
value of the 'type' in the `sumPricePerType` object to be this new sum,
rounded to two decimal points.
If it does not, set the value of the 'type' in the `sumPricePerType` object
to be the price of the current item, rounded to two decimal points.
4. Then, using a for-in loop on the `sumPricePerType` object, log the total
amount spent on each unique item type to the console. Write this for-in
loop after the for-of loop from pt. 2
Before and after the for-in loop, write a console.log to give this
information a header (like "Total sum for each type") and a footer ("--------")
to make the information easier to read in the console.
For Loop Exercise pt. 4
Purpose: We now want to see the total amount of money we spent on everything
1. Thankfully we already have a variable we can use to answer the above prompt!
Create a new variable named `totalSum` and set it to 0.
Then, using a for-in loop on `sumPricePerType`, sum up the spending totals
for each 'type' using the `totalSum` variable to hold the overall sum.
2. Then, after the for-in loop from pt. 3
log this information to the console, surrounded with a header that
says "Total sum for items purchased" and a footer that says "-------"
3. Finally, we want to see this amount printed in USD (U.S. Dollar)
formatting to the console.
Use Intl.NumberFormat with the proper options to edit the code written in
the step above to format the `totalSum` value as USD and print it to the console.
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line and code in this space";
// CODE IN THE OPEN LINES ABOVE
}