-
Notifications
You must be signed in to change notification settings - Fork 8
/
functions.js
261 lines (222 loc) · 7.13 KB
/
functions.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
/*
* ====Functions====
*
* Functions are great, they take data (input), do stuff with that data and then
* return it to you all shiny and fancy (output).
*
* Below are some specifications for Functions to be built.
*/
/*
* Declare Two Variables
*
* a random Number value
* b random Number value
* We will be using both of these variables to pass as parameters to the
* following functions that we will write. Pay close attention to the other
* variable names you will create as they will become input to other functions.
*/
var a = 52;
var b = 27;
console.log('a: ' + a);
console.log('b: ' + b);
/*
* Function - add
* This function returns the result of adding a and b together. Store this
* value in a variable named sum.
*/
function add(x, y) {
return x + y;
}
var sum = add(a, b);
console.log('\nsum: ' + sum);
/*
* Function - subtract
* This function returns the result of subtracting b from a Store this value in
* a variable named difference.
*/
function subtract(x, y) {
return x - y;
}
var difference = subtract(a, b);
console.log('\ndifference: ' + difference);
/*
* Function - multiply
* This function returns the result of multiplying b by a. Store this value in a
* variable named product.
*/
function multiply(x, y) {
return x * y;
}
var product = multiply(a, b);
console.log('\nproduct: ' + product);
/*
* Function - checkDifference
* This function accesses the value stored in the difference variable and uses
* this number to return the string "My football team lost X times this week",
* where X is the value stored in difference.
*/
function checkDifference(numTimesLost) {
return 'My football team lost ' + numTimesLost + ' times this week';
}
console.log('\ncheckDifference:');
// N.B. Return values can be passed directly to other functions
console.log(checkDifference(difference));
/*
* Function - checkSum
* This function checks the value stored at sum and uses that number to print to
* the screen the phrase "I CAN ADDZ X NUMBERS" where X is the value stored in
* the variable sum .
*/
function checkSum(x) {
console.log('I CAN ADDZ ' + x + ' NUMBERS');
}
console.log('\ncheckSum:');
checkSum(sum);
/*
* Function - checkProduct
* This function checks the value stored at product and multiplies it by the number stored at difference and then prints the result to the console.
*/
function checkProduct(x) {
// N.B. Functions can access variables defined outside of them!
console.log(x * difference);
}
console.log('\ncheckProduct:');
checkProduct(product);
/*
* Function - addThenSubtract
*
* This function takes three Number arguments named by you, then adds the first
* two arguments together. Then with the sum of that operation, subtract the
* value at the third argument.
*
* This function should make use of your previous functions.
*
* example: addThenSubtract(4, 5, 7); //-> returns 2 because 4 + 5 - 7 = 2
*/
function addThenSubtract(x, y, z) {
return subtract(add(x, y), z);
}
var c = 11;
console.log('\naddThenSubtract', a, b, c, '->', addThenSubtract(a, b, c));
/*
Function - addThenMultiply
This function takes three Number arguments named by you, then adds the first two arguments together. Then with the sum of that operation multiply it by the third argument. This function also should make use of your previous functions.
Store the return of this function to a variable named howMany
*/
/*
* Function - createFullName
* @param Datatype: String firstName
* @param Datatype: String lastName
* @return Datatype: String
*
* This function takes two String arguments firstName and lastName. This
* function returns back a string which represents someone's full name.
*
* Call this function and pass your first and last name into it. Store the
* return value to a variable named myFullName
*/
function createFullName(firstName, lastName) {
return firstName + ' ' + lastName;
}
var myFirstName = 'Marty';
var myLastName = 'McFly';
var myFullName = createFullName(myFirstName, myLastName);
// log to test...
// N.B. console.log can take a list and puts a space between value
console.log('\ncreateFullName "' + myFirstName + '" "' + myLastName + '" -> "' + myFullName + '"');
/*
* Function - verifyDrinkingAge
* @param Datatype: Number age
* @return Datatype: Boolean
*
* This function takes one Number argument age. This function returns the
* Boolean value true or false if age is lower than 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 canDrinkBeer
*/
function verifyDrinkingAge(age) {
return age >= 21;
}
var someonesAge = 22;
var canDrinkBeer = verifyDrinkingAge(someonesAge);
// log to test...
console.log('\nverifyDrinkingAge', someonesAge, '->', canDrinkBeer);
someonesAge -= 1;
canDrinkBeer = verifyDrinkingAge(someonesAge);
console.log('\nverifyDrinkingAge', someonesAge, '->', canDrinkBeer);
someonesAge -= 1;
canDrinkBeer = verifyDrinkingAge(someonesAge);
console.log('\nverifyDrinkingAge', someonesAge, '->', canDrinkBeer);
/*
* Function - throwParty
* This function checks the value stored at the canDrinkBeer variable and if
* the value is false it should print to a message to the screen, "The Party
* will have tons of Cake!" otherwise this message should be "This Party will
* have an open bar".
*/
function throwParty(oldEnough) {
if (oldEnough) {
console.log('This Party will have an open bar');
} else {
console.log('The Party will have tons of Cake!');
}
}
console.log('\nthrowParty ' + canDrinkBeer + ':');
throwParty(canDrinkBeer);
canDrinkBeer = true;
console.log('\nthrowParty ' + canDrinkBeer + ':');
throwParty(canDrinkBeer);
/*
* Function - eatFood
*
* This function takes 3 arguments firstName, lastName, food and prints out a
* message to your screen. Internally this function will make use of the
* createFullName function you created earlier.
*
*
* example 1.
*
* call: eatFood( "John", "Papa", "Pizza" )
*
* output: "John Papa loves to eat Pizza"
*
*
* example 2.
*
* call: eatFood( "Peter", "Bojangles", "California Burritos" )
*
* output: "Peter Bojanglesloves loves to eat California Burritos"
*/
function eatFood(firstName, lastName, food) {
var fullName = createFullName(firstName, lastName);
console.log(fullName + ' loves to eat ' + food + '!');
}
console.log('\neatFood:');
eatFood('Mark', 'Egesdal', 'pizza');
/*
* Function - repeater
*
* This function will print to the screen X amount of times. Where X is the
* value stored at howMany. Inside of this function it will check if the value
* of canDrinkBeer, if the value is true the message will be "Bacon Pancakes,
* makin' Bacon Pancakes..." othewise the message will be "Let it go.... LET IT
* GOOOOOOoOoOoOo..."
*/
function repeater(howMany, oldEnough) {
for (var i = 0; i < howMany; i++) {
if (oldEnough) {
console.log('Bacon Pancakes, makin\' Bacon Pancakes');
} else {
console.log('Let it go.... LET IT GOOOOOOoOoOoOo...');
}
}
}
var n = 5;
console.log('\nrepeater ' + n + ' ' + canDrinkBeer + ':');
repeater(n, canDrinkBeer);
n -= 2;
canDrinkBeer = false;
console.log('\nrepeater ' + n + ' ' + canDrinkBeer + ':');
repeater(n, canDrinkBeer);