-
Notifications
You must be signed in to change notification settings - Fork 3
/
exercises.js
543 lines (329 loc) · 12.2 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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
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
Comparison Operators
1. Create two variables that hold different numbers.
2. Then, check if one is bigger than the other using the
`>` (greater than) operator
3. Log the result to the console
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line of code and write your code here";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_02() {
/*
Exercise 2
Comparison Operators
1. Create two variables, one assigned to a number and one
assigned to a string. Then, log the result of comparing them
with the `===` (strict equality) operator
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line of code and write your code here";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_03() {
/*
Exercise 3
Conditionals
1. Create a variable and store an even or odd number, your choice.
2. Using the `%` (remainder) operator, write an if-else conditional
that checks if a number is even.
If it is even, log "Even" to the console.
If it is odd, log "Odd" to the console.
3. Modify your initial variable code to assign a different number,
this time the opposite type (even or odd) than it was at first.
Run your code again and see how the result changes.
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line of code and write your code here";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_04() {
/*
Exercise 4
Logical Operators
1. Create a variable and assign it a number between 10 and 20
2. Write an if statement, using the >= (greater than or equal to),
<= (lesser than or equal to), and && (AND conditional) operators
to check if this value is between 10 and 20.
If it is, have the message "Between 10 and 20" be logged to the console.
3. Modify your initial variable definition to be assigned a number
either less than 10 or greater than 20. Then run your conditional code
again. This time, nothing should log to the console.
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line of code and write your code here";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_05() {
/*
Exercise 5
Logical Operators
1. Create a variable named `color` that is assigned a color name as a string.
2. Create an if-else conditional statement that checks if the `color`
variable is either "red" or "blue".
If it is, log "Color is red or blue" to the console.
If it is not, log "Color is not red or blue" to the console
Reminder: The logical OR operator in JavaScript is `||`.
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line of code and write your code here";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_06() {
/*
Exercise 6
Logical NOT
1. Create a variable and assign it a Falsy value
2. Then, using the `!` (NOT conditional) operator,
log the result of ![variableName] to the console
Use `!` to check if a value is falsy and log the result
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line of code and write your code here";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_07() {
/*
Exercise 7
Mathematical Operators
1. Add two numbers and log the result.
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line of code and write your code here";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_08() {
/*
Exercise 8
Mathematical Operators
1. Subtract one number from another and log the result.
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line of code and write your code here";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_09() {
/*
Exercise 9
Mathematical Operators
1. Multiply two numbers and log the result.
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line of code and write your code here";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_10() {
/*
Exercise 10
Mathematical Operators and Conditional Statements:
1. Divide one number by another.
If it is even, log "Result is Even" to the console.
If it is odd, log "Result is Odd" to the console
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line of code and write your code here";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_11() {
/*
Exercise 11
Remainder Operator
1. Use the `%` (remainder) operator to find the
remainder of a division and log it.
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line of code and write your code here";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_12() {
/*
Exercise 12
Exponentiation
1. Use the `**` (exponentiation) operator to
calculate 2 to the power of 3 and log the result.
2. Then use the operator again to find the value of 11 to the power of 3.5.
Log the result
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line of code and write your code here";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_13() {
/*
Exercise 13
Increment and Decrement a Variable
1. Create a variable and assign it the number 5.
2. Increment the variable using the `++` operator, and log the variable.
3. Then, decrement the variable using the `--` operator twice.
Log the variable again.
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line of code and write your code here";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_14() {
/*
Exercise 14
Assignment Operators
1. Create a variable named `score` and assign it a value over 21.
2. Use the `+=` operator to add 14 to this variable and log the variable.
3. Then, use the `-=` to subtract 7 from this variable. Log the variable again.
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line of code and write your code here";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_15() {
/*
Exercise 15
Assignment Operators
1. Create a variable named `savings` and assign it a value over 500
2. Use the `*=` operator to multiply the variable by 1.08.
Log the variable to the console.
3. Then, use the `/=` operator to divide the variable by .5.
Log the variable to the console again.
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line of code and write your code here";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_16() {
/*
Exercise 16
Falsy Values
1. Write a conditional statement to check if an empty string is falsy.
If it is, log the message "An empty string is falsy" to the console
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line of code and write your code here";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_17() {
/*
Exercise 17
Truthy Values:
1. Write a conditional statement to check if a string value of "false" is falsy.
If it is, log the message "the string is falsy" to the console.
Otherwise, log the message, "the string is not falsy" to the console.
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line of code and write your code here";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_18() {
/*
Exercise 18
Objects
1. Create an object and assign it to a variable.
2. Then create another variable, and assign it the first variable as a value.
3. Then, using the `===` operator, check if the first and second varable
are strictly equal. Log the result to the console.
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line of code and write your code here";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_19() {
/*
Exercise 19
Strict vs. Loose Equality
1. Compare `undefined` and `null` using both `==` and `===`.
Log both results to the console.
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line of code and write your code here";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_20() {
/*
Exercise 20
Switch Statement:
1. Create a variable named `fruitColor` and
assign it a string for a color name.
2. Then, write a switch statement that will log "Apple" if the
fruitColor is red, "Banana" if the fruit color is yellow, and
"Unknown fruit" as a default condition.
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line of code and write your code here";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_21() {
/*
Exercise 21
Nested Conditionals:
1. Create a variable that stores a positive or negative number.
2. Then, create a nested conditional (using if, else if, and else) to
log "Positive" if the number is positive, "Negative" if the number
is negative, and "Zero" otherwise.
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line of code and write your code here";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_22() {
/*
Exercise 22
Ternary Operator
1. Create a variable named `age` and assign it a number between 12 and 22.
2. Then, create a variable named `canVote` that is assigned a ternary operator
with the following return conditions:
- if `age` is greater than or equal to 18, return "Yes, can vote"
- Otherwise, return "No, too young"
3. Then log the `canVote` variable to the console
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line of code and write your code here";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_23() {
/*
Exercise 23
Combining Logical Operators
1. Create a variable named `isProgramMember` and assign it a value of true or false.
2. Create a variable named `age` and assign it a value between 10 and 85
3. Write a condition that checks if isProgramMember is true AND that
their age is below 14 OR over 65
Write this condition in one line using parenthesis,
the OR `||` operator, and the AND `&&` operator.
4. If the condition returns true, log
"Eligible for special renewal discount" to the console.
Otherwise, log "Not eligible for special renewal discount" to the console.
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line of code and write your code here";
// CODE IN THE OPEN LINES ABOVE
}
function exercise_24() {
/*
Exercise 24
1. Create a variable named `day` and assign it the name of a day of the week.
2. Create a switch statement that uses cascading `case` statements to print
"Weekend" if the day falls on the weekend. Otherwise, print "Weekday" if
it does not.
*/
// CODE IN THE OPEN LINES BELOW
const placeholder = "Delete this line of code and write your code here";
// CODE IN THE OPEN LINES ABOVE
}