-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
287 lines (170 loc) · 5.73 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
/*
Exercise 1: Define an empty array
1) Create an empty array and assign it to a variable called `foods`.
Exercise 1 has been completed for you:
*/
const foods = [];
console.log('Exercise 1 result:', foods);
/*
Exercise 2: Add strings to the array
1) Add 'pizza' and 'cheeseburger' to the `foods` array.
Note: 'pizza' should be the first item in the array, followed by 'cheeseburger'.
Complete Exercise 2 in the space below:
*/
foods.push('pizza','cheeseburger')
console.log('Exercise 2 result:', foods);
/*
Exercise 3: Insert at the beginning
1) Insert the string 'taco' at the beginning of the `foods` array.
Complete Exercise 3 in the space below:
*/
console.log(foods.unshift('taco'))
console.log('Exercise 3 result:', foods);
/*
Exercise 4: Access an array element
1) Retrieve the 'pizza' string from the array based on its position (index) in
the array.
2) Assign it to a variable called `favFood`.
Complete Exercise 4 in the space below:
*/
console.log(foods[1]);
const favFood = foods[1];
console.log('Exercise 4 result:', favFood);
/*
Exercise 5: Insert an element between two others
1) Insert the string 'tofu' between 'pizza' and 'cheeseburger' in the array.
Complete Exercise 5 in the space below:
*/
foods.splice(2, 0, 'tofu');
console.log('Exercise 5 result:', foods);
/*
Exercise 6: Replace elements
1) Replace 'pizza' in the `foods` array with 'sushi' and 'cupcake'.
Complete Exercise 6 in the space below:
*/
foods.splice(1, 1, 'sushi', 'cupcake')
console.log('Exercise 6 result:', foods);
/*
Exercise 7: Using the `slice()` method
1) Use the `slice()` method to create a new array that contains 'sushi' and
'cupcake'.
2) Assign it to a variable named `yummy`.
Complete Exercise 7 in the space below:
*/
yummy = foods.slice(1,3);
console.log('Exercise 7 result:', yummy);
/*
Exercise 8: Finding an index
1) Using the `indexOf()` method, find the index of the string 'tofu' in the
`foods` array.
2) Assign it to a variable named `soyIdx`.
Complete Exercise 8 in the space below:
*/
let soyIdx = foods.indexOf('tofu')
console.log('Exercise 8 result:', soyIdx);
/*
Exercise 9: Joining elements
1) Use the `join()` method to concatenate the strings in the `foods` array,
separated by ' -> '.
2) Assign the result to a variable called `allFoods`.
Note: The final result should log as:
'taco -> sushi -> cupcake -> tofu -> cheeseburger'
Complete Exercise 9 in the space below:
*/
let allFoods = foods.join( ' -> ')
console.log('Exercise 9 result:', allFoods);
/*
Exercise 10: Check for an element
1) Using the .includes() method, check if the `foods` array contains the string
'soup'.
2) Assign the result to a variable called `hasSoup``.
Complete Exercise 10 in the space below:
*/
let hasSoup = foods.includes('soup')
console.log('Exercise 10 result:', hasSoup);
/*
Exercise 11: Odd numbers from an array
1) Choose a method to iterate through the `nums` array.
2) Push each odd number to a new array named `odds`.
Hint: Initialize the `odds` variable to an empty array before the iteration.
Complete Exercise 11 in the space below:
*/
const nums = [100, 5, 23, 15, 21, 72, 9, 45, 66, 7, 81, 90];
const odds = [];
for (let i=0; i < nums.length; i++) {
if (nums[i] % 2 !== 0) {
odds.push(nums[i]);
}
}
console.log('Exercise 11 result:', odds);
/*
Exercise 12: FizzBuzz with arrays
1) Choose a method to iterate through the `nums` array.
2. As you loop, sort the numbers into new arrays based on the following rules:
- Push any number evenly divisible by 3 to an array called `fizz`.
- Push any number evenly divisible by 5 to an array called `buzz`.
- Push any number that is evenly divisible by 3 and 5 to an array called
`fizzbuzz`.
Note: A single number may meet more than one of the above rules. If it does,
it should be placed in multiple arrays. For example, the number `15`
will appear in the `fizz`, `buzz`, and `fizzbuzz` arrays.
Complete Exercise 12 in the space below:
*/
const fizz = []
const buzz = []
const fizzbuzz = []
for (let i = 0; i < nums.length; i++) {
if (nums[i] % 3 === 0) {
fizz.push(nums[i]);
}
if (nums[i] % 5 === 0) {
buzz.push(nums[i]);
}
if (nums[i] % 3 === 0 && nums[i] % 5 === 0) {
fizzbuzz.push(nums[i]);
}
}
console.log('Exercise 12 Results:');
console.log(' fizz:', fizz);
console.log(' buzz:', buzz);
console.log(' fizzbuzz:', fizzbuzz);
/*
Exercise 13: Retrieve the Last Array
1) Assign the last nested array in the `numArrays` below to a variable named
`numList`. As you do this, also fulfill these goals:
- Assume you don't know how many nested arrays `numArrays` contains.
- Do not alter the original `numArrays` array.
Complete Exercise 13 in the space below:
*/
const numArrays = [
[100, 5, 23],
[15, 21, 72, 9],
[45, 66],
[7, 81, 90]
];
const numList = numArrays.slice(-1)[0]
console.log('Exercise 13 result:', numList);
/*
Exercise 14: Accessing within nested arrays
1) Retrieve the number `66` from the `numArrays` array. As part of this process
do not alter the original `numArrays` array.
2) Assign it to a variable called `num`.
Complete Exercise 14 in the space below:
*/
let num = numArrays[2][1]
console.log('Exercise 14 result:', num);
/*
Exercise 15: Nested array sum
1) Use nested loops or `forEach()` methods to sum up all numbers within
`numArrays` nested arrays.
2) Assign the sum to a variable called `total`.
Hint: Be sure to declare and initialize the total variable before the iterations.
Complete Exercise 15 in the space below:
*/
let total = 0
for (let i = 0; i < numArrays.length; i++) {
for (let j = 0; j < numArrays[i].length; j++){
total += numArrays[i][j];
}
}
console.log('Exercise 15 result:\n', total);