forked from devleagueprep/js-logic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solutions.js
533 lines (448 loc) · 13.3 KB
/
solutions.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
/*
If statements - Evaluates (or checks) a condition. If the condition is true, any statements in the subsequent code block are executed
*/
/*var today = new Date();
if(today === "Friday"){
return "Let's Party!";
};*/
/*
If/else statements = Evaluates (or checks) a condition. If the condition is true, the first code block is executed. If the condition is false, the second code block is executed instead.
*/
/*if(today === "Friday"){
return "Let's Party!";
}else{
return "Get back to coding!";
};*/
/*
* #1
* Function - canVote
* Create a function named `canVote` which will take a parameter: `age`.
*
* @param Datatype: Number `age`
* @return Datatype: Boolean
*
* The function will return true if the number passed into the function is equal to or greater than Hawaii's voting age. Console.log your result.
*/
function canVote(age){
if(age >= 18){
return true;
}else{
return false;
}
}
console.log("1. canVote: ", canVote(19));
/*
* #2
* Function - login
* Create a function named `login` which will take a parameter: `password`.
*
* @param Datatype: String `password`
* @return Datatype: String
*
* The function will return the message: "Login Success!", if the string passed into the function is "test1234"
* Console.log your result.
*/
function login(password){
if(password === "test1234"){
return "Login Success!";
}
}
console.log(login("test1234"));
/*
* #3
* Function - isGreaterThan
* Create a function named `isGreaterThan` which will take two parameters: `first` and `second`
*
* @param Datatype: Number `first`
* @param Datatype: Number `second`
* @return Datatype: Boolean
*
* The function will return true if the first number is greater than the second.
* Console.log your result.
*/
function isGreaterThan(first, second){
if(first > second){
return true;
}
}
console.log(isGreaterThan(3, 1));
var showResult = isGreaterThan(5, 1);
console.log("isGreaterThan: ", showResult);
/*
* #4
* Function - mustBeTrue
* Create a function named `mustBeTrue` which will take a parameter: `boo`.
*
* @param Datatype: Boolean `boo`
* @return Datatype: Boolean
*
* The function will return true if the value passed into the function is "true".
* Console.log your result.
*/
function mustBeTrue(boo){
if(boo === true){
return true;
}else{
return false;
}
}
console.log("mustBeTrue: ", mustBeTrue(true));
/*
* #5
* Function - bigBird
* Create a function named `bigBird` which will take a parameter: `word`.
*
* @param Datatype: String `word`
* @return Datatype: String
*
* The function will return the message "Word to Big Bird!", if the string passed into the function is a three-letter word.
* Console.log your result.
*/
function bigBird(word){
if(word.length === 3){
return "Word to Big Bird!";
}
}
console.log("bigBird: ", bigBird("cat"));
/*
* #6
* Function - isEqual
* Create a function named `isEqual` which takes two parameters: `first` and `second`.
*
* @param Datatype: String `first`
* @param Datatype: String `second`
* @return Datatype: String
*
* If the strings are equal, the function will return the message "You look mahvelous!" Otherwise, return the message: "I don't know who you are anymore."
* Console.log your result.
*/
function isEqual(first, second){
if(first === second){
return "You look mahvelous!";
}else{
return "I don't know who you are anymore.";
}
}
console.log(isEqual("ying", "yang"));
/*
* #7
* Function - notEqual
* Create a function named `notEqual` which takes two parameters: `first` and `second`.
*
* @param Datatype: String `first`
* @param Datatype: String `second`
* @return Datatype: String
*
* If the strings are not equal, the function will return the message "Opposites do attract." Otherwise, return the message: "Cause it's like you're my mirror."
* Console.log your result.
*/
function notEqual(first, second){
if(first !== second){
return "Opposites do attract.";
}else{
return "Cause it's like you're my mirror." ;
}
}
console.log(notEqual("sweet", "sour"));
/*
* #8
* Function - spareChange
* Create a function named `spareChange` which takes a parameter: `money`.
*
* @param Datatype: Number `money`
* @return Datatype: Boolean
*
* The function will return true if the number passed into the function is greater than 100, otherwise it will return false.
* Console.log your result.
*/
function spareChange(money){
if(money >100){
return true;
}else{
return false;
}
}
console.log("spareChange: ", spareChange(101));
/*
* #9
* Function - dirty30
* Create a function named `dirty30` which takes three parameters: `one`, `two` and `three`.
*
* @param Datatype: Number `one`
* @param Datatype: Number `two`
* @param Datatype: Number `three`
* @return Datatype: Boolean
*
* The function will return true if the sum of all the number values is greater than 30, otherwise it will return false.
* Console.log your result.
*/
function dirty30(one, two, three){
var sum = one + two + three;
if(sum > 30){
return true;
}else{
return false;
}
}
console.log("dirty30: ", dirty30(10, 20, 25));
/*
* #10
* Function - evenStevens
* Create a function named `evenStevens` which takes a parameter: `num`.
*
* @param Datatype: Number `num`
* @return Datatype: Boolean
*
* The function will return true if the number passed in is an even integer, otherwise it will return false.
* Console.log your result.
*/
function evenStevens(num){
if(num%2 === 0){
return true;
}else{
return false;
}
}
console.log("evenStevens ", evenStevens(8));
/*
* #11
* Function - daClub
* Create a function named `daClub` which takes two parameters: `cover` and `age`.
*
* @param Datatype: Number `cover`
* @param Datatype: Number `age`
* @return Datatype: String
*
* If BOTH values are 21 or over, the function will return the message: "Welcome to the Legends Lounge." Otherwise, it will return the message: "Chuck E Cheese is across the street."
* Console.log your result.
*/
function daClub(cover, age){
if(cover >= 21 && age >= 21){
return "Welcome to the Legends Lounge.";
}else{
return "Chuck E Cheese is across the street.";
}
}
console.log("daClub: ", daClub(22, 21));
/*
* #12
* Function - graduation
* Create a function named `graduation` which takes two parameters: `credits` and `thesis`.
*
* @param Datatype: Number `credit`
* @param Datatype: Boolean `thesis`
* @return Datatype: String
*
* If EITHER the number value is greater than or equal to 120 or the boolean value is true, then the function will return the message: "Congratulations on a job well done." Otherwise, return the message: "See you in summer school."
* Console.log your result.
*/
function graduation(credits, thesis){
if(credits >= 120 || thesis === true){
return "Congratulations on a job well done.";
}else{
return "See you in summer school.";
}
}
var gradResults = graduation(100, true);
console.log("graduuation: ", gradResults);
/*
* #13
* Function - moneyTrain
* Create a function named `moneyTrain` which takes a parameter: `speed`.
*
* @param Datatype: Number `speed`
* @return Datatype: String
*
* The function will return the message: "You are riding Honolulu's Rail.", if the number value is less than 50, otherwise it will return the message: "You are riding an Amtrak.", if the number value is less than 100, and return the message: "Now you ballin' in the Shinkansen!", if the number value is greater than or equal to 100.
* Console.log your result.
*/
function moneyTrain(speed){
if(speed < 50){
return "You are riding Honolulu's Rail.";
}else if(speed < 100){
return "You are riding an Amtrak.";
}else{
return "Now you ballin' in the Shinkansen!";
}
}
console.log(moneyTrain(99));
/*
* #14
* Function - buyDoughnut
* Declare a variable named `budget` and assign it with a number value that is greater than 20.
* Declare a variable named `doughnutPrice` and assign it with a number value that is greater than 0 but less than 6.
* Declare a variable named `doughnutBought` and assign it with a number value of 0.
*
* Create a function named `buyDoughnut` which takes NO parameters.
* When the function is invoked, the budget will be decreased by the doughnutPrice and doughnutBought will increase by 1.
* Console.log budget and doughnutBought.
* Invoke your function again.
* Console.log budget and doughnutBought again.
*/
var budget = 100;
var doughnutPrice = 5;
var doughnutBought = 0;
function buyDoughnut(){
if(budget >= doughnutPrice){
budget -= doughnutPrice;
doughnutBought++;
}
}
buyDoughnut();
console.log("budget ", budget);
console.log("bought ", doughnutBought);
/*Final Boss*/
/*Create a function name dailySpecials which takes in a parameter: `special`.
Inside the function, create a switch statement that will check the daily specials of your favorite restaurant (or make up your own daily specials for each day of the week.*/
function dailySpecials(special) {
switch (special) {
case 'Monday':
menu = 'Sweet Onion Chicken Teriyaki';
break;
case 'Tuesday':
menu = 'Oven Roasted Chicken';
break;
case 'Wednesday' :
menu = 'Turkey';
break;
case 'Thursday' :
menu = 'Italian BLT';
break;
case 'Friday' :
menu = 'Tuna';
break;
}
return menu;
}
console.log(dailySpecials('Friday'));
/*
For loops - A for loop checks a condition a specific number of times and allows us to execute a code block and evaluate a condition to determine if our loop should run again.
The for loop is made up for 3 parts:
1) Initialization (i.e. var i = 0;)
2) Condition (i.e. i<arr.length;)
3) Update (i.e. i++)
*/
var toyotaModels = ["Corolla", "Prius", "4 Runner", "Camry", "Land Cruiser"];
for (var i = 0; i<toyotaModels.length; i++){
console.log("Toyota " + toyotaModels[i]);
}
/*
* #15
* Create a for loop that will iterate 5 times and console.log the following:
* "Player: 1"
* "Player: 2"
* "Player: 3"
* "Player: 4"
* "Player: 5"
*/
for(var i = 1; i<=5; i++){
console.log("player ", i);
}
/*
* #16
* Create a for loop that will iterate and console.log each item in the array below:
*/
var myFavFoods = ["lemon bar", "carrot cake", "nachos", "bacon cheeseburger", "ramen", "sweet potato fries", "chimichanga"];
for(var i = 0; i<myFavFoods.length; i++){
console.log(myFavFoods[i]);
}
/*
* #17
* Function - sumItUp
* Declare a variable named `numArray` and assign it with an array of 5 random numbers of your choice.
* Declare a variable named `total` and assign it with a number value of 0.
*
* Create a function named sumItUp which takes a parameter: `arr`.
*
* @param Datatype: Array `arr`
* @return Datatype: Number
*
* The function will loop through and add up all the values in the array that is passed into the function and return the total.
* Console.log your result.
*/
var numArray = [22, 33, 44, 55, 66];
function sumItUp(arr){
var total = 0;
for(var i = 0; i<arr.length; i++){
console.log(arr[i]);
total += arr[i]; // total = total + arr[i];
}
return total;
}
console.log("sumitup ", sumItUp(numArray));
/*
* #18
* Function - allStars
* Create a function named `allStars` which takes a parameter: `ballers`.
*
* @param Datatype: Array `ballers`
* @return Datatype: Array
*
* The function will loop through the players array and will put all the even number indexed players in the `east` array and the rest in the `west` array.
* Console.log both the east and west arrays.
*/
var players = ["Yao Ming", "BrookLin", " Jesus Shuttlesworth", "Manute Bol", "Sidney Deane", "World B Free"];
var east = [];
var west = [];
function allStars(ballers){
for(var i = 0; i<ballers.length; i++){
console.log(ballers[i]);
if(i%2 === 0){
//console.log(i);
east.push(ballers[i]);
}else{
west.push(ballers[i]);
}
}
}
allStars(players);
console.log("east: ", east);
console.log("west: ", west);
/*
* #19
* Function - subways
* Create a function named `subways` which takes a parameter: `special`.
*
* @param Datatype: Array `special`
* @return Datatype: Array
*
* The function will loop through the array value and replace all the odd numbered indexed items with "Classic Tuna".
* Console.log your results.
*/
var subOftheDay = ["Teriyaki Chicken", "Spicy Italian", "Turkey", "BMT", "Black Forest Ham", "Meatball Marinara", "Veggie"];
function subways(special){
for(var i = 0; i<special.length; i++){
if(i%2 === 1){
console.log(i);
special.splice(i, 1, "Classic Tuna");
special[i] = "Classic Tuna";
}
}
return special;
}
subways(subOftheDay);
console.log(subOftheDay);
/*
Final Boss
* #20
* Function - removeLetter
* Create a function named `removeLetter`, which takes a parameter `str`.
*
* @param Datatype: String `str`
* @return Datatype: Array
*
* The function will loop through the string value and put all the letters into an array, except for the letter "A" and "a". We don't want no stinking "A" or "a" in our array. Test your function with the `phrase` below!
*/
var phrase = "An apple a day keeps Alice feeling awesome!";
function removeLetter(str){
var newArr = [];
for(var i = 0; i<str.length; i++){
console.log(str[i]);
if(str[i] !== 'a' && str[i] !== "A"){
newArr.push(str[i]);
}
}
console.log(newArr);
return newArr;
}
removeLetter(phrase);