-
Notifications
You must be signed in to change notification settings - Fork 2
/
pt1-basics.js
461 lines (294 loc) · 19.3 KB
/
pt1-basics.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
/*
Welcome to the beginning of this Javascript crash course. This first part is only testing the basics so it should be fairly easy.
I'm hoping things will get harder (and more challenging!) as you move further through the course.
READ ME FIRST:
Whenever you see // CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //, delete that code and write your code there.
Follow the instructions and stop when you see a giant code block that tells you to stop.
When you're done with the entire lesson, run the code in Node.
Try your BEST to use ES6 syntax. Let/Const vs var. Arrow functions, etc.
Chapter 1: The Basics.
Here we're gonna work on basic Javascript syntax. Most of these will be simple variable assignments.
*/
// Part 1: Strings:
// 1A. Using a 'let' statement, define the variable 'color' with your favorite color!
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// 1B. Define the variable 'food' with your favorite food. Make it so this variable assignment cannot change.
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// 1C. Use the code below. Make it all caps and assign it to a variable evenHappier.
const happy = 'i am super excited'
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// 1D. Using a method, remove all the extra spaces on the variable extraSpaces and assign it to variable noExtraSpaces
const extraSpaces = ' so many extra spaces. DansGame. ';
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// 1E. Using a method or methods, remove all periods and replace them with spaces. There are two ways to do this. We can call this properSpaces1 and properSpaces2.
const improperSpaces = 'please.turn.these.periods.into.spaces.'
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// Part 2: Numbers:
// 2A. Define a variable 'age', which is your birthday.
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// 2B. Using Math methods, find the minimum and maximum of the following numbers. Define them in numbersMin and numbersMax.
// 5, 6, 3, 4, 7, 8, 1, 2
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// 2C. Find the absolute value of -47 and assign it to numbersAbs.
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// 2D. Assign a random 4 digit number (no decimals!) to variable randomFourDigitNumber
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// Part 3: Equality + Truthy/Falsey
// 3. Assign variables equality1, equality2, etc and make them all true. (This might seem a little convoluted);
// Example: equality0: Use the greater symbol.
// equality0 = 2 > 5;
// 3A. equality1: Use the less symbol.
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// 3B. equality2: Use the greater than symbol.
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// 3C. equality3: Use the triple equal sign.
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// 3D. equality4: Use addition and the === sign.
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// 3E. equality5: Use false.
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// 3F. equality6: Use undefined.
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// 3G. equality7. Use a && statement.
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// 3H. equality8. Use a || statement.
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// Part 4: Arrays
// 4A. Create a variable colorsArray and put five colors in your array.
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// 4B. Assign the third color in your array and assign it to favoriteColor.
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// The next challenges will be using the moreNumbers. DO NOT ALTER the array.
const moreNumbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
// 4C. Make a new copy of the array and assign it to newNumbers.
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// 4D. Use a method to reverse the array and set that to variable reversedNumbers.
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// 4E. Use a method to return the fifth through 8th (inclusive) and assign it to someNumbers.
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// 4F. Add the number 11 to the end of newNumbers.
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// 4G. Add the number -2 and -1 to the front of newNumbers.
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// 4H. Assign to variable newNumbersLength the length of newNumbers.
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// Part 5: Objects
// 5A. Make an empty object literal and assign it to variable 'favorites'.
// Use dot notation to add the property 'food' and assign it your favorite food.
// Use dot notation to add the property 'color' and assign it your favorite color.
// Use bracket notation to add the property 'song' and assign it your favorite song.
// Use bracket notation to add the property 'quote' and assign it your favorite quote.
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// 5B. Assign to variable 'myself' with an object literal with the following properties.
// firstName, lastName, dob, age, occupation
// also include the favorites object with property favorites.
// 5C. Add a method to the 'myself' object called greet that logs a greeting with firstName and lastName.
// Then invoke it.
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// Part 6: Loops
// 6A. Use a for loop to iterate over the string, 'loopString', and individually print each letter.
const loopString = 'This string is for looping!';
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// 6B. Use a for loop to iterate over the array, 'loopArray' and individually print each element.
const loopArray = ['white', 'blue', 'black', 'red', 'green'];
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// 6C. Use a for loop to iterate over the object, 'loopObject', and individually print each key and property.
const loopObject = {
vietnamese: 'pho',
chinese: 'chow mein',
japanese: 'ramen',
korean: 'kalgooksu'
};
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// 6D. Use a while loop to print numbers 1 - 10.
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// Part 7: Functions
// 7A. Write a function called cube that returns the cube of a number.
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// 7B. Write a function called reverse that takes in a string as an argument and returns the string reversed.
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// 7C. Write a function called searchForElement that takes in a array and a key as arguments. find returns true if the key is found, false if not.
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// Part 8. Putting it all together
// Everything in Part 8 will be used together.
// Try your best not to reference anything else!
/*
Let's put everything together now.
8A. Create an array 'favoriteFoods', containing five strings. Each element should be some sort of food you enjoy.
Log the third item within this array to the console.
*/
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
/*
Now let's do something with this array.
8B. Create a function 'print', It takes in a string as an argument.
Assuming we pass in 'ramen' to the function, it will print to the console:
'One of my favorite things is ramen'
*/
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
/*
Now let's call it.
8C. Call print on each element in the favoriteFoods array.
*/
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
/*
Calling this five times isn't very DRY (Don't Repeat Yourself) How can we minimize this?
8D. Use a loop to iterate through the array and call print on each element.
*/
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
/* Cool. Let's do some more stuff.
8E. Create an array, favoriteFruits, and fill this with your favorite fruits.
Now call print on each element of favoriteFruits using a loop again.
*/
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
/*
It looks like we're repeating this loop over and over again. Let's create a function to keep our code DRY.
8F. Create a function called printEach. It takes in an array as an argument.
printEach will iterate over the array, then call print on each element of the array.
*/
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
/*
Let's see what we have now.
8G. Call printEach on favoriteFoods. Then call printEach again but on the favoriteFruits array.
*/
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
/*
Now we're going to create another function.
8H. Create a function called printWithExclaims.
printWithExclaims takes a string and will add three exclamation marks to the end of the string.
Example: steak!!!
*/
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
/*
Let's create another function that includes loops.
8I. Create a function called printEachWithExclaims.
Will do the exact same thing printEach does, but will call printWithExclaims instead.
*/
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
/*
Awesome. We're almost done!
8J. Call printEachWithExclaims on both favoriteFoods and on favoriteFruits.
*/
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
/*
Looks like printEach and printEachWithExclaims still aren't very DRY.
When you think about it, they keep repeating the loop.
Let's settle this once and for all.
8K. Create a function 'each'. It takes in two arguments: an array and a function.
It iterates through the array and calls the function on every element of the array.
It passes in the leement as an argument for the function being called.
Basically, we want to be able to call 'each' and pass in 'favoriteFoods' or 'favoriteFruits', or any other array;
as well as any function we've created.
This lets us be versatile on what we want to do, so we don't have to set a new function to do something every single time.
*/
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
/*
Last one, then you're done with Chapter 1.
8L. Call 'each' on both favoriteFruits and favoriteFoods. Passing in two functions we made earlier.
*/
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// CODE GOES HERE // // CODE GOES HERE // // CODE GOES HERE //
// CONGRATULATIONS. YOU FINISHED CHAPTER ONE!
/*
STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE.
STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE.
STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE.
STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE.
STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE.
STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE.
STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE.
STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE.
STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE.
STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE.
STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE.
STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE.
STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE.
STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE.
STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE. STOP CODING HERE.
*/
// Strings
console.log('STRINGS:');
console.log('1A: ', typeof color === 'string', ' color: ', color);
console.log('1B: ', typeof food === 'string', ' food: ', food);
console.log('1C: ', evenHappier.toUpperCase() === evenHappier, ' evenHappier: ', evenHappier);
console.log('1D: ', noExtraSpaces[0] !== ' ', ' noExtraSpaces: ', noExtraSpaces);
console.log('1E: ', properSpaces.indexOf('.') === -1, ' properSpaces: ', properSpaces);
console.log('\n');console.log('\n');console.log('\n');
// Numbers
console.log('Numbers:');
console.log('2A: ', numbersMin === 1, ' numbersMin: ', numbersMin);
console.log('2B: ', numbersMax === 8, ' numbersMax: ', numbersMax);
console.log('2C: ', numbersAbs === 47, ' numbersAbs: ', numbersAbs);
console.log('2D: ', randomFourDigitNumber.toString().length === 4, ' randomFourDigitNumber: ', randomFourDigitNumber);
console.log('\n');console.log('\n');console.log('\n');
// Equality
console.log('Equality: ');
console.log('3A: ', equality1);
console.log('3B: ', equality2);
console.log('3C: ', equality3);
console.log('3D: ', equality4);
console.log('3E: ', equality5);
console.log('3F: ', equality6);
console.log('3G: ', equality7);
console.log('3H: ', equality8);
console.log('\n');console.log('\n');console.log('\n');
// Arrays
console.log('Arrays: ');
console.log('4A: ', colorsArray.length === 5, ' colorsArray: ', colorsArray);
console.log('4B: ', colorsArray[2] === favoriteColor, ' favoriteColor: ', favoriteColor);
console.log('4C: ', newNumbers.toString() === [-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11].toString(), ' newNumbers: ', newNumbers);
console.log('4D: ', reversedNumbers.toString() === moreNumbers.slice(0).reverse().toString(), ' reversedNumbers: ', reversedNumbers);
console.log('4E: ', someNumbers.toString() === moreNumbers.slice(5, 9).toString(), ' someNumbers: ', someNumbers);
console.log('4F: ', newNumbers[newNumbers.length - 1] === 11);
console.log('4G: ', newNumbers[0] === -2 && newNumbers[1] === -1);
console.log('4H: ', newNumbersLength === 14, ' newNumbersLength: ', newNumbersLength);
console.log('\n');console.log('\n');console.log('\n');
// Objects
console.log('Objects: ');
console.log('5A: ', !!favorites && !!favorites.food && !!favorites.color && !!favorites.song && !!favorites.quote, ' favorites: ', JSON.stringify(favorites));
console.log('5B: ', !!myself && !!myself.firstName && !!myself.lastName && !!myself.age && !!myself.dob && !!myself.occupation && !!myself.favorites, ' myself: ', JSON.stringify(myself));
console.log('5C: ', typeof myself.greet === 'function');
myself.greet();
console.log('\n');console.log('\n');console.log('\n');
// Loops
// no tests for loops
console.log('Loops: ');
console.log('No tests for loops');
console.log('\n');console.log('\n');console.log('\n');
// Functions:
console.log('Functions: ');
console.log('7A: ', cube(3) === 27);
console.log('7B: ', reverse('abc') === 'cba');
console.log('7C: ', searchForElement([1, 2, 3], 2) === true && searchForElement([1, 2, 3], 4) === false );
console.log('\n');console.log('\n');console.log('\n');
// Putting it all together:
console.log('Putting it all together: ');
console.log('each: function? ', typeof each === 'function');
let eachTest = [1, 2, 3];
each(eachTest, elem => { return elem * 2 });
console.log('each: works? ', eachTest.toString() === [2, 4, 6].toString());