-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctionDrillsLab.js
More file actions
532 lines (387 loc) · 14.9 KB
/
functionDrillsLab.js
File metadata and controls
532 lines (387 loc) · 14.9 KB
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
// Function Drills Exercise //
/*
Some of the following questions will ask you to use
arrow function syntax. On the problems that don't,
feel free to practice with any syntax.
*/
////////////////// PROBLEM 1 ////////////////////
/*
Create a function called helloWorld which simply console logs 'Hello, World!'
Call the function.
*/
//CODE HERE
const helloWorld = () => {
console.log('Hello, World');
}
helloWorld();
console.log('----------------------------');
////////////////// PROBLEM 2 ////////////////////
/*
Write an arrow function called 'jsNinja' that returns the string: 'I am a JavaScript ninja!'
*/
//CODE HERE
const jsNinja = () => {
console.log('I am a JavaScript ninja!');
}
jsNinja();
console.log('----------------------------');
////////////////// PROBLEM 3 ////////////////////
/*
Create a function called printName which takes in a person's name and console logs it.
Ex. If 'Cameron' were passed in as the argument, Cameron would be console logged.
Call the function, passing in an argument.
*/
//CODE HERE
const printName = (name) => {
console.log(name);
}
printName('Joy');
console.log('----------------------------');
////////////////// PROBLEM 4 ////////////////////
/*
Create a function called greeting that
accepts name as its only parameter.
greeting should log the string 'Hello, '
plus the value of the name parameter.
Ex. If Jake were passed in as the argument, the function would log 'Hello, Jake'
Make sure to call your function and pass in an argument.
*/
//CODE HERE
const greeting = (name) => {
name = String(name) //correction
console.log('Hello, ' + name); //My answer
console.log(`Hello, ${name}`) //Their answer
}
greeting('Joy');
console.log('----------------------------');
////////////////// PROBLEM 5 ////////////////////
/*
Write an arrow function called 'compareNums' that takes in 2 parameters,
which will be numbers.
The function should return the bigger number.
If the numbers are the same, just return the number.
Brownie points if you use a ternary statement (only spend significant time on this if you have wiggle room)
*/
//CODE HERE
// const compareNums = (num1, num2) => {
// if (num1 > num2) {
// console.log(num1)
// } else if (num1 < num2) {
// console.log(num2)
// }
// }
// compareNums(57, 16);
const compareNums = (num1, num2) => num1 > num2 ? console.log(num1) : console.log(num2);
// const compareNums = (num1, num2) => num1 > num2 ? num1 : num2 another way to write
compareNums(57, 16);
console.log('----------------------------');
////////////////// PROBLEM 6 ////////////////////
/*
Create a function called add that takes in two parameters
Inside, convert the arguments to be numbers (just in case strings get sent in)
The add function should RETURN the two parameters added together.
Create a variable outside the function called 'sum' and set it equal to add invoked (called), passing in 2 arguments.
*/
//CODE HERE
const add = (param1, param2) => {
param1 = Number(param1);
param2 = Number(param2);
return param1 + param2;
}
let sum = add('7', '3');
console.log(sum);
// Another way
// function add(num, num2) {
// num = +num
// num2 = +num2
// return num + num2
// }
// let sum = add(12, 24)
console.log('----------------------------');
////////////////// PROBLEM 7 ////////////////////
/*
Which syntax was used to create the function below?
Uncomment the correct `console.log` underneath.
*/
const exclaim = function (str) {
return str.toUpperCase() + '!!!'
}
// console.log('arrow')
// console.log('declaration')
console.log('expression');
console.log('----------------------------');
////////////////// PROBLEM 8 ////////////////////
/*
Which syntax was used to create the function below?
Uncomment the correct `console.log` underneath.
*/
const exclaimTwo = str => {
return str.toUpperCase() + '!!!'
}
console.log('arrow')
// console.log('declaration')
// console.log('expression')
/*
Rewrite exclaimTwo to be a single line.
Call your new function exclaimThree
Brownie points if you use a template string
*/
// const exclaimThree = str => str.toUpperCase() + '!!!'
const exclaimThree = str => `${str.toUpperCase()}!!!` //Template string way
console.log(exclaimThree('joy'));
console.log('----------------------------');
////////////////// PROBLEM 9 ////////////////////
/*
Which syntax was used to create the function below?
Uncomment the correct `console.log` underneath.
*/
function exclaimFour(str) {
return str.toUpperCase() + '!!!'
}
// console.log('arrow')
console.log('declaration')
// console.log('expression')
console.log('----------------------------');
////////////////// PROBLEM 10 ////////////////////
/*
Write a function called nameCheck that takes in a name parameter.
nameCheck should check if the name equals 'Steven'. If it does, return 'What is up Steven?'
If the name parameter is equal to Bryan, return 'Hey Bryan!'
If the name parameter is anything else, return 'Cool name, NAMEPARAM' (with NAMEPARAM being the value of the name parameter being passed in).
Create a variable called 'nameGreeting' and set it equal to your function invoked (called) passing in an argument.
*/
//CODE HERE
const nameCheck = (NAMEPARAM) => {
if (NAMEPARAM === 'Steven') {
return 'What is up Steven?'
} else if (NAMEPARAM === 'Bryan') {
return 'Hey Bryan'
} else {
return 'Cool name, ' + NAMEPARAM
}
}
let nameGreeting = nameCheck('Joy')
console.log(nameGreeting);
console.log('----------------------------');
////////////////// PROBLEM 11 ////////////////////
/*
Write a function called faveColorFinder that takes in one parameter called color (which will be a string).
If the passed in color equals 'red', return 'red is a great color'
If the passed in color equals 'green', return 'green is a solid favorite color'
If the passed in color equals 'black', return 'so trendy'
Otherwise, you should return the string 'you need to evaluate your favorite color choice'
Create a variable called 'colorRating' and set it equal to faveColorFinder invoked (called), passing in an argument.
*/
//CODE HERE
const faveColorFinder = (color) => {
if (color === 'red') {
return 'red is a great color'
} else if (color === 'green') {
return 'green is a solid favorite color'
} else if (color === 'black') {
return 'so trendy'
} else {
return 'you need to evaluate your favorite color choice'
}
}
let colorRating = faveColorFinder('green');
console.log(colorRating);
console.log('----------------------------');
////////////////// PROBLEM 12 ////////////////////
let namesArr = ['Cameron', 'Riley', 'Eric', 'Brenna', 'Karl']
/*
Create a function called printAllNames that takes in a single argument (an array of names).
Using a for loop, iterate over that array and console log each name.
Call the function, passing in the `namesArr` array (above).
*/
//CODE HERE
const printAllNames = (namesArr) => {
for (let i = 0; i < namesArr.length; i++) {
console.log(namesArr[i]);
}
}
printAllNames(namesArr);
console.log('----------------------------');
////////////////// PROBLEM 13 ////////////////////
/*
Create a function called thatsOdd that takes in a single argument (a number).
Using conditional logic, if the number is even, return 'That's not odd!'
Otherwise, return 'That is odd indeed!'
Outside the function, create a variable called `oddChecker` and set it equal to your function invoked, making sure to pass in an argument.
*/
//CODE HERE
const thatsOdd = (number) => number % 2 === 0 ? "That's not odd!" : "That is odd indeed!"
let oddChecker = thatsOdd(95);
console.log(oddChecker);
console.log('----------------------------');
////////////////// PROBLEM 14 ////////////////////
/*
Write a one line arrow function called 'bestMovie' that takes in one parameter,
which will be a string of a movie title.
The function should return the string: 'MOVEIEPARAM is the best movie ever!'.
For example, if we passed in 'Sharknado',
we would expect the function to return 'Sharknado is the best movie ever!'
*/
//CODE HERE
const bestMovie = MOVEIEPARAM => MOVEIEPARAM + ' is the best movie ever!'
// const bestMovie = title => `${title} is the best movie ever!` // template string example
console.log(bestMovie('Real Steel'))
console.log('----------------------------');
////////////////// PROBLEM 15 ////////////////////
let bigOrSmallArray = [1, 101, 102, 2, 103, 4, 5, 6, 107]
/*
Create a function called 'bigOrSmall' that takes in one parameter, 'arr', which will be an array of numbers.
Inside of the bigOrSmall function, create a new array called 'answers'.
Then, loop over the passed in arr parameter, and check to see if the number in the array is GREATER than 100.
If it is, push 'big' as a string to the answers array.
If the number is LESS than or EQUAL to 100, push 'small' as a string to the answers array.
Return the answers array inside of the function to a variable called `arrayEvaluator`.
*/
//CODE HERE
const bigOrSmall = (arr) => {
let answers = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i] > 100) {
answers.push('big');
} else {
answers.push('small');
}
}
return answers;
}
let arrayEvaluator = bigOrSmall(bigOrSmallArray);
console.log(arrayEvaluator);
console.log('----------------------------');
////////////////// PROBLEM 16 ////////////////////
let contestants = ['Katniss', 'Peeta', 'Fox-face', 'Glimmer', 'Glimmer', 'Cato', 'Rue', 'Thresh', 'Clove', 'Marvel']
let loser = 'Glimmer'
/*
Write a function that is called theEliminator, which takes in two arguments, contestants (which will each be an array of strings), and loser (which will be a string).
The function should loop over the array of contestant names. If the loser string appears in the array, splice it out. Return the new contestants array.
*/
//CODE HERE
// const theEliminator = (arr, loser) => {
// for (let i = 0; i < arr.length; i++) {
// if (arr[i] === loser) {
// arr.splice(i, 1);
// i--;
// }
// }
// return console.log(arr);
// }
const theEliminator = (contestants, loser) => {
for (let i = 0; i < contestants.length; i++) {
if (contestants[i].includes(loser)) {
contestants.splice(i, 1);
i--;
}
}
return contestants;
}
let updatedContestants = theEliminator(contestants, loser);
console.log(updatedContestants);
console.log('----------------------------');
////////////////// PROBLEM 17 ////////////////////
let sampleString = "Hi, my name is Kylo."
/*
Write a function that takes in one argument, a string. The function should then console.log that string, in entirely uppercase characters.
Invoke the function, passing in the sampleString (above).
*/
//CODE HERE
const stringTaker = str => console.log(str.toUpperCase());
stringTaker(sampleString);
console.log('----------------------------');
////////////////// PROBLEM 18 ////////////////////
/*
Write a function called emailCheck that takes in
one parameter - email.
Inside the function, convert the email param into
a string and trim off any excess whitespace.
Check to make sure the email contains an '@' symbol.
If it does, return 'email verified' and if doesn't,
return 'must provide a valid email address'
*/
let sampleEmail = ' email@example.com ';
const emailCheck = (email) => {
email = String(email).trim();
if (email.includes('@')) {
return 'email verified'
} else {
return 'must provide a valid email address';
}
}
console.log(emailCheck(sampleEmail));
console.log('----------------------------');
////////////////// PROBLEM 19 ////////////////////
/*
Write a function, naming it whatever you believe to be appropriate, that buys as many chocolate frogs as possible with a certain amount of gold. Each chocolate frog costs 3 gold. Your function should take in a single parameter, which is the amount of gold you are willing to spend. Your function should return a total amount of chocolate frogs you were able to purchase.
Create a variable called `totalFrogs` and set it equal to your function invoked, passing in the amount of gold you are willing to spend.
*/
//CODE HERE
const chocolateFrogs = (gold) => {
return gold / 3;
};
let totalFrogs = chocolateFrogs(100);
console.log(totalFrogs);
console.log('----------------------------');
////////////////// PROBLEM 20 ////////////////////
/*
You might have noticed a slight bug in the previous problem. If you were to pass in 4 gold, the function would return to you 1.3333... However, you can't really go to a store and by 1.333 products. You would just be able to purchase 1 product. Re-write the function you used in the previous problem (give it the same name, just add a 2 to the end of it) that fixes this bug. Invoke the function and store the returned value to a variable called `totalFrogs2`.
*/
//CODE HERE
const chocolateFrogs2 = (gold) => {
if (gold % 3 === 0) {
return gold / 3
} else if ((gold - 1) % 3 === 0) {
return (gold - 1) / 3
} else {
return (gold - 2) / 3
}
}
let totalFrogs2 = chocolateFrogs2(100);
console.log(totalFrogs2);
console.log('----------------------------');
////////////////// PROBLEM 21 ////////////////////
let sampleArray = [0, 1, 2, 3, 4, 7, 5, 6, 8, 9]
/*
Write a function that takes in an array of numbers as an argument. In the body of the function, write logic to determine if the array is in ascending order. The function should return true, if it is sorted in ascending order, false if it is not. Create a variable, `arrayIsAscending` and set it equal to your function invoked. Use the sample array to test this function.
*/
//CODE HERE
const ascendingArrayChecker = arr => {
let comparisonValue = arr[0];
for (let i = 1; i < arr.length - 1; i++) {
if (arr[i] <= comparisonValue) {
return false;
} else {
comparisonValue = arr[i];
}
}
return true;
}
const arrayIsAscending = ascendingArrayChecker(sampleArray);
console.log(arrayIsAscending);
console.log('----------------------------');
////////////////// PROBLEM 22 ////////////////////
let duck = "cute";
function bathroom() {
let rubberDuck = "squeaky";
function bathtub() {
let sailorDuck = "nautical";
}
}
function pond() {
let realDuck = "fluffy";
}
/*
There are 4 variables above: duck, rubberDuck, sailorDuck and realDuck.
All within different scopes.
Given the functions and variables above, edit the arrays below to contain only the appropriate variable names (as strings).
*/
//This array should contain the variable names (as strings) accessible in the global scope.
let globalScope = ["duck"]
//This array should contain the variable names (as strings) accessible in the bathroom function.
let bathroomScope = ["duck", "sailorDuck"]
//This array should contain the variable names (as strings) accessible in the bathtub function.
let bathtubScope = ["duck", "sailorDuck", "rubberDuck"]
//This array should contain the variable names (as strings) accessible in the pond function.
let pondScope = ["duck", "realDuck"]