forked from devleagueprep/js-basics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solutions.js
375 lines (295 loc) · 10.8 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
// Comments are text that will not be executed in your code. Rather comments are for other programmers to read.
// Single line comments start with //
/* Multiple line comments start with*/ /* and end with */
/* All of the exercises below are commented out. Write your Javascript code after each exercise. */
/* Variables and Data Types */
/*Console.log each variable and test your code in the terminal using the Node REPL*/
/*i.e.*/
var pet = "sebastian the pug";
console.log(pet); // <= this should print out sebastian the pug
/*
* #1
* Variables with a "String" value
* Declare variables named firstName, lastName, birthPlace, favFood, favDrink, favSong, favAnimal, favColor, favSport, favDoughnut.
* Assign your own string values to each variable and console.log each variable.
*/
var firstName = 'Ronald';
console.log(firstName);
var lastName = 'McDonald';
console.log(lastName);
var birthPlace = 'San Bernardino, CA';
console.log(birthPlace);
var favFood = 'Big Macs';
console.log(favFood);
var favDrink = 'Coke';
console.log(favDrink);
var favSong = 'Mo Money Mo Problems';
console.log(favSong);
var favAnimal = 'Grimmace';
console.log(favAnimal);
var favColor = 'red';
console.log(favColor);
var favSport = 'Corn Hole';
console.log(favSport);
var favDoughnut = 'Krispy Kreme';
console.log(favDoughnut);
/*
* #2
* Variables with a Number value
*
* Declare variables named favNumber, yourShoeSize, thatOnePrinceSong, floorsAlaMoanaHotel, numOfJapanPrefectures, numOfABCstoresinHi, and cheesecakesFlavAtCheeseCakeFac
*
* Assign your own number values to each variable and console.log each variable.
*/
var favNumber = 36899;
console.log(favNumber);
var yourShoeSize = 11;
console.log(yourShoeSize);
var thatOnePrinceSong = 7;
console.log(thatOnePrinceSong);
var floorsAlaMoanaHotel = 38;
console.log(floorsAlaMoanaHotel);
var numOfJapanPrefectures = 47;
console.log(numOfJapanPrefectures);
var numOfABCstoresinHi = 57;
console.log(numOfABCstoresinHi);
var cheesecakesFlavAtCheeseCakeFac = 34;
console.log(cheesecakesFlavAtCheeseCakeFac);
/*
* #3
* Variables with a Boolean value
*
* Declare variables named likesMcDonalds, eatsDoughnuts and ownsRedShoes.
*
* Assign your own boolean values to each variable and console.log each variable.
*
*
* Now, use comparison and logic operators (>, <, >=, <=, ===, !==) to make the following variables True of False
*
* Console.log each variable.
*
*/
var likesMcDonalds = true;
console.log(likesMcDonalds);
var eatsDoughnuts = false;
console.log(eatsDoughnuts);
var ownsRedShoes = true;
console.log(ownsRedShoes);
//For example:
//Make me True:
var booya1 = 3 > 2;
console.log('booya1:' , booya1);
//Make me False:
var booya2 = 5<2;
console.log('booya2:' , booya2);
//Make me True:
var booya3 = 10 >= 10;
console.log('booya3:' , booya3);
//Make me False:
var booya4 = 11 <= 9;
console.log('booya4:' , booya4);
//Make me True:
var booya5 = 'Mayor McCheese' === 'Mayor McCheese';
console.log('booya5:' , booya5);
//Make me False:
var booya6 = 'hamburglar' !== 'hamburglar';
console.log('booya6:' , booya6);
/*
* #4
* Variables with a Null value
*
* Declare variables named completedPrepClass, traveledToMars and buyVicADrink.
*
* Assign a null value to each variable and console.log each variable.
*/
var completedPrepClass = null;
console.log(completedPrepClass);
var traveledToMars = null;
console.log(traveledToMars);
var buyVicADrink = null;
console.log(buyVicADrink);
/*
* #5
* Variables with a undefined value.
*
* Declare a variable named superBowlChamps, nextPres and hawaiiRail.
*
* Do not assign a value to the variable and console.log each variable.
*/
var superBowlChamps;
console.log(superBowlChamps);
var nextPres;
console.log(nextPres);
var hawaiiRail;
console.log(hawaiiRail);
/*
* #6
* Variables with an Array value
*
* Declare a variable named plateLunch and assign it an array containing 5 of your favorite lunch items.
* Declare a variable named `donutBox` and assign it an array containing 5 donuts of your choosing.
* Declare a variable named `westCoast` and assign it an array containing states found on the west coast of the US.
* Declare a variable named `lotteryResult and assign it an array containing 5 random numbers.
* Declare a variable named `hamajang` and assign it an array containing 6 different data types.
* Declare a variable named `dynamicDuos` and assign it an array containing 3 arrays, with each array containing items that match with one another.
*
* Console.log each variable.
*/
var plateLunch = ['mac salad', 'orange chicken', 'garlic chicken', 'meat jun', 'kalbi'];
console.log(plateLunch);
var donutBox = ['old fashion', 'buttermilk', 'pb and chocolate', 'coffee cake', 'poi malasada'];
console.log(donutBox)
var westCoast = ['California', 'Oregon', 'Washington'];
console.log(westCoast);
var lotteryResult = [11, 8, 2, 88, 3];
console.log(lotteryResult);
var hamajang = ['string', 0, ['this', 'is', 'an', 'arrray'], null, 5>3, 6<2, undefined];
console.log(hamajang);
var dynamicDuos = [['peanut butter', 'chocolate'], ['gin', 'tonic'], ['orange', 'chicken']];
console.log(dynamicDuos);
//*7
//Accessing values in Arrays
var partyList = ["carrot cake", "gin & tonic", "kalua pork nachos", "double stuff oreos", "cool ranch doritos", "orange chicken"];
// Console.log the entire array.
// Console.log the length of this array.
// Console.log only "carrot cake" from this array.
// Console.log only "cool ranch doritos" from this array
console.log(partyList);
console.log(partyList.length);
console.log(partyList[0]);
console.log(partyList[4]);
/*#8
* Concatenation
* Concatenate the variables `firstName` and `lastName` from exercise 1 and store them into a new variable called `fullName` (don't forget to include a space between the firstName and lastName).
*
* Using the fullName and birthPlace variables, console.log the following:
*
* "Hi my name is fullName and I was born in birthPlace."
*
* i.e "Hi my name is Bruce Wayne and I was born in Gotham."
*/
var fullName = firstName + ' ' + lastName;
console.log(fullName);
console.log('Hi name is is ' + fullName + ' and I was born in ' + birthPlace + '.');
/*
* #9
* Arithmetic
* Variables with the outcome of an arithmetic operation.
* Declare two variables, `num1` and `num2` and assign each of these variables a number of your choosing.
* Next perform the following arithmetic operations:
* Add the two variables and store it to new variable named sum.
* Subtract the two variables and store it to a new variable named difference.
* Mulitply the two variables and store it to a new variable named product.
* Divide the two variables and store it to a new variable named quotient.
* Find the remainder (modulus) of the two variables and store in to a new variable names leftOver.
*
* Console.log each new variable.
*/
var num1 = 9;
var num2 = 11;
var sum = num1 + num2;
console.log('sum:' , sum);
var difference = num1 - num2;
console.log('difference:' , difference);
var product = num1 * num2;
console.log('product:' , product);
var quotient = num1 / num2;
console.log('quotient:' , quotient);
var leftOver = num1 % num2;
console.log('modulus:' , leftOver);
/*
* #10
* Comparisons & Logic
* Variables with the outcome of a comparison operation:
*
* Declare a variable named `isStrictlyEqual` and compare two strings "Tacocat" and "tacocat" using the strict equality (===).
* Declare another variable named `isLooselyEqual` and compare the two strings "Tacocat" and "tacocat" using equality (==).
* Declare a variable named `sameNum` and compare a string "5" and a number 5 using strict equality.
* Declare another variable named `sameNumba` and compare a string "5" and a number 5 using equality.
* Console.log the variables. Do you see the difference between strict equality vs. equality?
*/
var isStrictlyEqual = 'Tacocat' === 'tacocat';
console.log(isStrictlyEqual);
var isLooselyEqual = 'Tacocat' == 'tacocat';
console.log(isLooselyEqual);
var sameNum = '5' === 5;
console.log(sameNum);
var sameNumba = '5' == 5;
console.log(sameNumba);
/*
* #11
* Logical AND Logical OR
* Use comparison and logic operators (>, <, >=, <=, ===, !==) with the logical && and logical || to make the following variables True of False:
*/
//Use && to make me True
var booya7 = 10>5 && 3<8;
console.log('booya7:' , booya7);
//Use && to make me False
var booya8 = 5>4 && 5<4;
console.log('booya8:', booya8)
//Use || to make me True
var booya9 = 9<11 || 9>11;
console.log('booya9:' , booya9);
//Use || to make me False
var booya10 = 7>8 || 8>9;
console.log('booya10:' , booya10);
//Console.log each variable
/*
* #12
* Assignments
* Variables with the outcome of an assignment operation (+=, -=, *=, /=, %=):
*
* Declare a variable named `myNum` and assign it with a number of your choosing.
*
* Change the value of the muNum variable by using the assignment operators and console.log myNum after each assignment.
*/
var myNum = 19;
console.log(myNum);
//increment and assign 3 (+=):
myNum += 3;
console.log(myNum);
//decrement and assign 1 (-=):
myNum -= 1;
console.log(myNum);
//multiple and assign 2 (*=);
myNum *= 2;
console.log(myNum);
//divide and assign 5 (/=);
myNum /= 5;
console.log(myNum);
//modulo and assign 4 (%=);
myNum %= 4;
console.log(myNum);
/*===================================================================*/
/*
* Final Boss
* The following exercises are designed to further challenge you and give you a jump start on the next topic that'll be covered. Let the boss battle begin!
*
* Function - A block of code designed to perform a particular task. It allows you to reuse code.
*
* Declare a function named bakePie which has a single parameter ingredient.
*
* When the function is invoked, it should return a string, "Today's special is ingredient pie." Where ingredient will be the value that you pass into the function.
*
* For example, if the value is `blueberry`, it should return "Today's special is blueberry pie."
*
* Declare a variable pieResult and assign it to the function call.
* Console.log the variable to see the result.*/
/*
* Declare a Function named jump which has a single parameter, height.
*
* This function should return a string, "You jumped height feet high!" Where height will be the value that you pass into the function.
*
* For example, if the value is 9, it should return "You jumped 9 feet high!"
* Declare a variable jumpResult and assign it to the function call.
* Console.log the variable to see the result.
*/
/*
* Declare a Function named makeFood which takes three parameters, ingredient1, ingredient2, recipeName.
*
* This function should return a string ingredient1 and ingredient2 make a recipeName. Where ingredient1, ingredient2 and recipeName will be the values that you pass into the function.
*
* For example, if the values are "Cheese", "Bacon", "pancake" it should return "Cheese and bacon make a pancake."
*
* Declare a variable foodResult and assign it to the function call. Console.log the variable to see the result.
*/