-
Notifications
You must be signed in to change notification settings - Fork 3
/
exercises.js
440 lines (279 loc) · 9.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
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: Function Declarations and Naming Conventions
Problem:
Write a function declaration named calculateArea that takes the radius of a
circle as a parameter and returns the area of the circle.
The formula for the area of a circle: PI * radius * radius
Use proper naming conventions for functions in JavaScript.
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line and code here";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_02() {
/*
Exercise 2: Function Expressions and Their Uses
Problem:
Convert the calculateArea function from Exercise 1 into
a function expression and assign it to a variable called calculateArea.
Run it and log the output
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line and code here";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_03() {
/*
Exercise 3: Arrow Functions and How They Differ from Traditional Functions
Problem:
Rewrite the calculateArea function as an arrow function.
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line and code here";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_04() {
/*
Exercise 4: Using Functions to Validate and Transform Data
Problem:
Write a function isValidEmail that takes a string and returns true
if it is a valid email address, and false otherwise.
For simplicity, consider an email valid if it contains
exactly one @ symbol and at least one . after the @.
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line and code here";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_05() {
/*
Exercise 5: Function Parameters, Including Default Values
Problem:
Write a function 'greet' that takes a name and an optional greeting message.
If no greeting is provided, it should default to 'Hello'.
The function should return a greeting message.
Then, log the return of this function to the console
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line and code here";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_06() {
/*
Exercise 6: Understanding Local and Global Scope
Problem:
Consider the following code. What will be logged to the console and why?
Write what you think in a code comment. Then run the code.
*/
let message = "Global";
function showMessage() {
let message = "Local";
console.log(message);
}
showMessage();
console.log(message);
/*
Write what you think will happen IN THIS COMMENT BLOCK.
Then explain why the code behaved as it did.
*/
}
function exercise_07() {
/*
Exercise 7: How Function Scope Works and Variable Limitations
Problem:
Write a function 'incrementCounter' that declares a variable 'counter'
initialized to 0 inside it, increments 'counter' by 1, and logs it to the console.
Call incrementCounter twice.
What will be logged each time and why can't we access
counter outside the function? Provide your answer in a code comment below
the function.
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line and code here";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_08() {
/*
Exercise 8: Functions That Return Other Functions
Problem:
Write a function 'makeMultiplier' that takes a 'multiplier' number
and returns a new function that takes a number and
returns that number multiplied by 'multiplier'.
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line and code here";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_09() {
/*
Exercise 9: Concepts and Use Cases for Recursive Functions
Problem:
Write a recursive function 'factorial' that takes a
non-negative integer 'n' and returns its factorial.
If 'n' is a negative number, log "Bad number input" to the console, and
`return` from the function.
This will prevent an infinite loop from occuring.
In the future, we can use error handling techniques to handle these use cases
in a cleaner manner.
Info on what a factorial is: https://www.cuemath.com/numbers/factorial/
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line and code here";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_10() {
const fruits = ["apple", "banana", "cherry"];
/*
Exercise 10: Using forEach to Iterate Over Arrays
Problem:
Given the array `fruits` defined above, use forEach to
print each fruit to the console.
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line and code here";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_11() {
/*
Exercise 11: Replacing Traditional Loops with Functional Approaches
Problem:
Replace the following for loop with a functional approach using
the reduce() method to calculate the sum of an array of numbers.
*/
// REPLACE the code below
const numbers = [1, 2, 3, 4, 5];
let sum = 0;
for (let i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
console.log(sum); // Outputs: 15
// REPLACE the code above
}
function exercise_12() {
const numbers = [10, 15, 20, 25, 30];
/*
Exercise 12: Filtering Arrays with `filter`
Problem:
Given the array of numbers above, use the filter() method to
create a new array containing only the even numbers.
Log this new array to the console
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line and code here";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_13() {
/*
Exercise 13: How JavaScript Handles Primitives and
Objects Differently in Memory
Problem:
Explain why modifying an object passed to a function affects
the original object, but modifying a primitive value does not.
Provide code examples to illustrate this behavior.
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line and code here";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_14() {
/*
Exercise 14: Reference Types and How They Are Passed Around
Problem:
Write code to demonstrate that objects are passed by reference by creating
a function that adds a new property to an object passed as an argument.
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line and code here";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_15() {
const users = [
{ id: 1, name: "Alice" },
{ id: 2, name: "Bob" },
{ id: 3, name: "Charlie" },
];
/*
Exercise 15: Working with Arrays using 'find'
Problem:
Given the array of user objects above, use the find() method to
locate the user with the name 'Bob'.
Print this user object to the console
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line and code here";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_16() {
const person = {
name: "John Doe",
age: 30,
occupation: "Engineer",
};
/*
Exercise 16: Using Object.entries to Iterate Over Object Properties
Problem:
Given the 'person' object above, use Object.entries and .forEach to iterate
over the object's properties and print each key and value.
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line and code here";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_17() {
/*
Exercise 17: Using Functions to Transform Data
Problem:
Write a function 'capitalizeStrings' that takes an array of strings and
returns a new array with each string in title case
(first letter uppercase, rest lowercase).
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line and code here";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_18() {
/*
Exercise 18: Using Default Parameters
Problem:
Write a function 'power' that calculates the power of a number.
The function should take two parameters: base and exponent,
with exponent defaulting to 2 if not provided.
You can use Math.pow in your function to find the power of a number:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/pow
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line and code here";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_19() {
/*
Exercise 19
Immediate Function:
Write an IIFE(Immediately Invoked Function Expression) that
that logs information to the console.
Here's a page with more details on IIFEs:
https://developer.mozilla.org/en-US/docs/Glossary/IIFE
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line and code here";
// CODE IN THE OPEN LINES ABOVE
}