forked from devleagueprep/js-functions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solutions.js
401 lines (326 loc) · 11.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
/*
* A function is a block of code (a series of instructions) designed to execute a certain task. Functions allow you to reuse code.
* i.e. A function to generate a greeting to specific person you designate:*/
// Step One: Define the function.
function greeting(guest){ // takes in a parameter named 'guest' which acts like a local variable inside the function
return "Good afternoon " + guest + "."; // the output value after the function is invoked
}
greeting("Mr. Smith"); // invoking or calling the function to execute the code (instructions) inside the function.
// Step Two: Assign the function invocation to a variable.
var testGreeting = greeting("Mr. Smith"); // assigning the function call into a variable
// Step Three: Console.log the variable to test function output!
console.log(testGreeting); // printing the output value of the function.
// From your terminal, try running this file with node in order to see the result of `console.log(testGreeting);`
/*Below are some specifications for Functions to be built. */
/*
* #1
* Declare two variables
* @variable Datatype: Number `bango1`
* @variable Datatype: Number `bango2`
*
* These two variables will be used to invoke the functions #2 - #5.
*/
var bango1 = 8;
var bango2 = 3;
/*
* #2
* Function - add
* Create a function named `add` which will take two parameters: `num1` and `num2`.
*
* @param Datatype: Number `num1`
* @param Datatype: Number `num2`
* @return Datatype: Number
*
* This function will add two numbers (the parameters, `num1` and `num2`) and
* return the sum of these two numbers.
* Invoke the function and assign it to a variable named `sum`.
* Console.log `sum` to test your code.
*/
function add(num1, num2){
return num1 + num2;
}
var sum = add(bango1, bango2);
console.log("sum: ", sum);
/*
* #3
* Function - subtract
* Create a function named `subtract` which will take two parameters: `num1` and `num2`.
*
* @param Datatype: Number `num1`
* @param Datatype: Number `num2`
* @return Datatype: Number
*
* This function will subtract two numbers and return the difference of these two numbers.
* Invoke the function and assign it to a variable named `difference`.
* Console.log `difference` to test your code.
*/
function subtract(num1, num2){
return num1 - num2;
}
var difference = subtract(bango1, bango2);
console.log("difference: ", difference);
/*
* #4
* Function - multiply
* Create a function named `multiply` which will take two parameters: `num1` and `num2`.
*
* @param Datatype: Number `num1`
* @param Datatype: Number `num2`
* @return Datatype: Number
*
* This function will multiply two numbers and return the product of these two numbers.
* Invoke the function and assign it to a variable named `product`.
* Console.log `product` to test your code.
*/
function multiply(num1, num2){
return num1 * num2;
}
var product = multiply(bango1, bango2);
console.log("product: " , product);
/*
* #5
* Function - divide
* Create a function named `divide` which will take two parameters: `num1` and `num2`.
*
* @param Datatype: Number `num1`
* @param Datatype: Number `num2`
* @return Datatype: Number
*
* This function will divide two numbers and return the quotient of these two numbers.
* Invoke the function and assign it to a variable named `quotient`.
* Console.log `quotient` to test your code.
*/
function divide(num1, num2){
return num1 / num2;
}
var quotient = divide(bango1, bango2);
console.log("quotient: " , quotient);
/*
* #6
* Function - checkSum
* Create a function named `checkSum` which will take a parameter: `x`.
*
* @param Datatype: Number `x`
* @return Datatype: String
*
* This function will return the string "Mariah Carey has been married `x` amount of times." Where `x` is the value when you invoke the function. Invoke this function using the variable `daDiva`.
* Console.log your result.
*/
function checkSum(x){
return "Mariah Carey has been married " + x + " amount of times.";
}
console.log(checkSum(sum));
/*
* #7
* Function - checkDifference
* Create a function named `checkDifference` which will take a parameter `x`.
*
* @param Datatype: Number `x`
* @return Datatype: String
*
* This function will return the string "Last night I dreamt that I ate `x` Taco Bell soft tacos." Where `x` is the value when you invoke the function. Invoke this function using the variable `difference`.
* Console.log your results.
*/
function checkDifference(x){
return "Last night I dreamt that I ate " + x + " Taco Bell soft tacos";
}
console.log(checkDifference(difference));
/*
* #8
* Function - checkProduct
* Create a function named `checkProduct` which does not require any parameters.
*
* @return Datatype: Number
*
* This function will multiply the values stored in the sum and product.
* Console.log your result.
*/
function checkProduct(){
return sum * product;
}
var showResult = checkProduct();
console.log("showResult: " , showResult);
/*
* #9
* Function - checkQuotient
* Create a function named `checkQuotient` which does not require any parameters.
*
* @return Datatype: Number
*
* This function will multiply the values stored in the product and quotient.
* Console.log your result.
*/
function checkQuotient(){
return product * quotient;
}
console.log("checkQuotient: " , checkQuotient());
/*
* #10
* Function - addThenSubtract
* Declare three variables
*
* @variable Datatype: Number `bango3`
* @variable Datatype: Number `bango4`
* @variable Datatype: Number `bango5`
*
* Next, create a function named addThenSubtract which takes three parameters: `num1`, `num2`, `num3`.
*
* @param Datatype: Number `num1`
* @param Datatype: Number `num2`
* @param Datatype: Number `num3`
* @return Datatype: Number
*
* This function will add the first two parameters together. Then with the sum of that operation, subtract it from the third parameter. PLEASE USE YOUR PREVIOUS FUNCTIONS FOR THIS EXERCISE.
* Console.log your result.
*/
var bango3 = 5;
var bango4 = 6;
var bango5 = 9;
//ver 1
function addThenSubtract(num1, num2, num3){
var sum = add(num1, num2);
return subtract(sum, num3);
}
console.log("addThenSubtract: ", addThenSubtract(bango3, bango4, bango5));
//ver 2
function addThenSubtract(num1, num2, num3){
return subtract(add(num1, num2), num3);
}
console.log("addThenSubtract: ", addThenSubtract(bango3, bango4, bango5));
/*
* #11
* Function - multiplyThenDivide
* Create a function named multiplyThenDivide which takes three parameters: `num1`, `num2`, `num3`.
*
* @param Datatype: Number `num1`
* @param Datatype: Number `num2`
* @param Datatype: Number `num3`
* @return Datatype: Number
*
* This function will multiply the first two parameters together. Then with the product of that operation, divide it from the third parameter. PLEASE USE YOUR PREVIOUS FUNCTIONS FOR THIS EXERCISE.
* Console.log your result.
*/
function multiplyThenDivide(num1, num2, num3){
return divide(multiply(num1, num2), num3);
}
console.log("multiplyThenDivide: " , multiplyThenDivide(bango3, bango4, bango5));
/*
* #12
* Function - createFullName
* Create a function named createFullName which takes two parameters: `firstName`, `lastName`.
*
* @param Datatype: String `firstName`
* @param Datatype: String `lastName`
* @return Datatype: String
*
* This function `returns` back a string which represents someone's full name. Invoke this function by passing in your first and last name into the function. Store the return value into a variable named `myFullName` and console.log this variable to show your result.
*/
function createFullName(firstName, lastName){
return firstName + " " + lastName;
}
var myFullName = createFullName("Bruce", "Lee");
console.log(myFullName);
/*
* #13
* Function - eatFood
* Create a function named eatFood which takes three parameters: `firstName`, `lastName`, `food`.
*
* @param Datatype: String `firstName`
* @param Datatype: String `lastName`
* @param Datatype: String `food`
* @return Datatype: String
*
* This function will use the previous `createFullName` function above to create a message. For example, if you invoked the function like this: eatFood("Biggie", "Smalls", "Won Ton Soup"); it will display the message "Biggie Smalls eats Won Ton Soup everyday for breakfast."
* Console.log your result.
*/
function eatFood(firstName, lastName, food){
return createFullName(firstName, lastName) + " eats " + food + " everyday for breakfast.";
}
console.log(eatFood("Biggie", "Smalls", "Won Ton Soup"));
/************** ENGAGE HYPERDRIVE **************/
/* No more training wheels! For the exercises #14-18, use the experience you've
* gained figure out how to build the necessary functions. Use your google-fu to
* figure out anything you don't know! Ganbatte (good luck)! */
/************** FIRE ALL CANNONS ***************/
/*
* #14
* Function shoeSize
* Create a function that will take in a parameter named `inches`. This function will convert inches to centimeters(cm).
* Console.log your result.
*/
function shoeSize(inches){
var cm = inches * 2.54;
return cm;
}
var inches = 5;
console.log(shoeSize(inches));
console.log(shoeSize(8));
/*
* #15
* Function allCaps
* Create a function that will take in a parameter named `str`.
* This function will capitalize all the letters in the string.
* Console.log your result.
* Example input: "believe you can and you're halfway there."
* Example output: "BELIEVE YOU CAN AND YOU'RE HALFWAY THERE."
*/
var believe = "believe you can and you're halfway there.";
function allCaps(str){
return str.toUpperCase();
}
console.log(allCaps(believe));
/*
* #16
* Function oneCap
* Create a function that will take in a parameter named `str`.
* This function will capitalize only the first letter in the variable phase above.
* Console.log your result.
*/
//ver 1
function oneCap(str){
return str.charAt(0).toUpperCase() + str.slice(1);
}
console.log(oneCap(believe));
//ver 2
function oneCap(str){
return str[0].toUpperCase() + str.slice(1);
}
console.log(oneCap(believe));
//ver 3
function oneCap(str){
return str[0].toUpperCase() + str.substring(1);
}
console.log(oneCap(believe));
/*
* #17
* Function - verifyDrinkingAge
* Create a function named verifyDrinkingAge which takes a parameter named `age`.
* This function returns a Boolean value, `true` or `false`, depending on
* whether `age` is above or below the legal drinking age in the state of Hawaii.
*
* Call this function and pass in a number value.
* Store the return value to a variable named `canDrink`. Console.log your result.
*/
function verifyDrinkingAge(age){
if(age >= 21){
return true;
}else{
return false;
}
}
var canDrink = verifyDrinkingAge(21);
console.log(canDrink);
/**
* #18
* Function - throwParty
* Create a function named throwParty. This function will check the value stored in the `canDrink` variable in the previous exercise. If the value is true, it will return the message "Cheee Hoo! We going to da party!" If false, then it will return the message "Meh, see you at Starbucks." Store the return value to a variable named `canParty`. Console.log your result.
*/
function throwParty(){
if(canDrink === true){
return "Cheee Hoo! We going to da party!";
}else{
return "Meh, see you at Starbucks.";
}
}
var canParty = throwParty();
console.log(canParty);