-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_ConditionalsExercise.js
288 lines (267 loc) · 9.89 KB
/
2_ConditionalsExercise.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
// In these exercises, you will be working on conditional statements,
// namely the if / else if / else and switch/case conditionals.
// For these exercises, you will place your code in a function block.
// If you don't know what a function is yet, that's ok, your code will be the
// same. The variables will be defined for you, you just have to plug in the
// logic.
// For example, if asked for exercise 0 to assign the value of myAnswer to the sum
// of num1 and num2, and were given this construct:
// function exercise0(num1, num2) {
// let myAnswer;
// // ------------------------------------------
// // Write your code for exercise 0 below here:
// // ------------------------------------------
//
// // ------------------------------------------
// // And above here
// // ------------------------------------------
// return myAnswer;
// }
// You would put your code between the "below here"
// and "above here" blocks as shown:
// function exercise0(num1, num2) {
// let myAnswer;
// // ------------------------------------------
// // Write your code for exercise 0 below here:
// // ------------------------------------------
// myAnswer = num1 + num2;
// // ------------------------------------------
// // And above here
// // ------------------------------------------
// return myAnswer;
// }
// First try answering these without using references or looking up any information.
// Then, check your answer by using references and/or running your code.
// You can run your JS code using the Chrome or Firefox Developer tools, or by using Node.js.
// Feel free to update your answers if you got them wrong at first -- this exercise is for your own learning.
// But make sure you understand why the correct answer is right.
// EXERCISE 1.
// First reassign the value of `answer1` so that it has the value of:
// "num1 is small"
// Write an if statement that checks if a variable `num1` is greater than 10.
// If it is, then within your if statement code change the value of answer1 so
// that its new value would be a string that says:
// "The value of num1 is <num1 value> and is greater than 10".
function exercise1(num1) {
let answer1 = "";
// ------------------------------------------
// Write your code for exercise 1 below here:
// ------------------------------------------
if (num1 > 10){
answer1 = `The value of num1 is ${num1} and is greater than 10`
}
// ------------------------------------------
// And above here
// ------------------------------------------
return answer1;
}
// EXERCISE 2.
// Write an if/else conditional statement that if given a number will assign
// a string value of:
// "<num2 value> is even" to `answer2`,
// if `num2` is even
// and a value of "<num2 value> is odd" to `answer2`,
// if `num2` is odd.
// ie. if num2 has a value of 4 then the message should read:
// "4 is even"
function exercise2(num2) {
let answer2;
// --------------------------------------------
// Write your code for the exercise below here:
// --------------------------------------------
if (num2 % 2 === 0){
answer2 = `${num2} is even`
} else{
answer2 = `${num2} is odd`
}
// --------------------------------------------
// And above here
// --------------------------------------------
return answer2;
}
// EXERCISE 3.
// Write an if/else if/else block such that if `num3` is positive, then
// answer3 is assigned the string value of:
// "<num3 value> is positive"
// if `num3` is negative, then the value should be:
// "<num3 value> is negative"
// otherwise if the value is zero it should return
// "<num3 value> is zero"
function exercise3(num3) {
let answer3;
// --------------------------------------------
// Write your code for the exercise below here:
// --------------------------------------------
if (num3 > 0){
answer3 = `${num3} is positive`
}
else if (num3 < 0){
answer3 = `${num3} is negative`
} else if (num3 === 0) {
answer3 = `${num3} is zero`
}
// --------------------------------------------
// And above here
// --------------------------------------------
return answer3;
}
// EXERCISE 4.
// Write an if/else statement such that if `varA` and `varB` are strings or
// numbers and they have equal values, then change the value of answer4 to
// "varA and varB are equal"
// otherwise assign a value of "varA and varB differ"
function exercise4(varA, varB) {
let answer4;
// --------------------------------------------
// Write your code for the exercise below here:
// --------------------------------------------
if (varA === varB){
answer4 = "varA and varB are equal"
} else {
answer4 = "varA and varB differ"
}
// --------------------------------------------
// And above here
// --------------------------------------------
return answer4;
}
// EXERCISE 5.
// In exercise 4, what are some of the unexpected cases where `varA` and `varB`
// seemed like they are equal, but would not pass the tests? In your analysis
// consider other data types beside strings and variables.
//The integar 0 and the boolean true should be equal but it wouldn't pass the test because they're not strictly equal.
// EXERCISE 6.
// Here, assign the value of true to answer6 if:
// `varA` and `varB` are equal, but `varA` or `varB` does not equal `varC`
// in the event this is not the case, change the value of answer6 to false
function exercise6(varA, varB, varC) {
let answer6 = "unassigned";
// --------------------------------------------
// Write your code for the exercise below here:
// --------------------------------------------
if (varA === varB && varA != varC && varB != varC){
answer6 = true;
} else {
answer6 = false;
}
// --------------------------------------------
// And above here
// --------------------------------------------
return answer6;
}
// EXERCISE 7.
// Use a switch conditional statement with case clauses such that if `num7` is
// a number and it has a value of 1 that `answer7` is assigned the string:
// "You won!"
// if num7 is 7, then answer7 should be:
// "You are lucky!"
// if num7 is 101, then answer7 should be:
// "Welcome to coding 101!"
// if num7 is 1000000, then answer7 should be:
// "You are one in a million!"
// Othewise, assign answer7 a value of:
// "Thanks for that!"
function exercise7(num7) {
let answer7;
// --------------------------------------------
// Write your code for the exercise below here:
// --------------------------------------------
if (typeof(num7) === "number" && num7 === 1){
answer7 = "You won!"
} else if (num7 === 7){
answer7 = "You're lucky!"
} else if (num7 === 101){
answer7 = "Welcome to coding 101!"
} else if (num7 === 1000000){
answer7 = "You are one in a million!"
} else {
answer7 = "Thanks for that!"
}
// --------------------------------------------
// And above here
// --------------------------------------------
return answer7;
}
// EXERCISE 8.
// Using any conditional assign the value of true to answer8 if:
// the values of amount1 and amount2 are between the values of
// minimum and maximum
// if not, assign a value of false to answer8
function exercise8(amount1, amount2, minimum, maximum) {
let answer8;
// --------------------------------------------
// Write your code for the exercise below here:
// --------------------------------------------
if (maximum > amount1 && amount1 > minimum && maximum > amount2 && amount2 > minimum){
answer8 = true;
} else {
answer8 = false;
}
// --------------------------------------------
// And above here
// --------------------------------------------
return answer8;
}
// EXERCISE 9.
// In this exercise, if `item` is a number, follow the rules given in Exercise 7
// except that `answer7` is replaced by `answer9`
// If `item` is not a number, then assign a value to answer9 of:
// "Please send a number, that was a <data type>."
// for example, if item===true, the value should be:
// "Please send a number, that was a boolean."
function exercise9(item) {
let answer9;
// --------------------------------------------
// Write your code for the exercise below here:
// --------------------------------------------
if (typeof(item) === "integar" && item === 1){
answer9 = "You won!"
} else if (item === 7){
answer9 = "You're lucky!"
} else if (item === 101){
answer9 = "Welcome to coding 101!"
} else if (item === 1000000){
answer9 = "You are one in a million!"
} else if (typeof(item) === "number"){
answer9 = "Thanks for that!"
} else {
answer9 = `Please send a number, that was a ${typeof(item)}`
}
// --------------------------------------------
// And above here
// --------------------------------------------
return answer9;
}
// EXERCISE 10.
// This question is a modified version of a classic programming question
// called "Fizz Buzz"
// Using a conditional, assign a value of:
// "Fizz" to `answer10` if the value of `num10` is divisible by 3
// "Buzz" to `answer10` if the value of `num10` is divisible by 5
// "Fizz Buzz" to `answer10` if the value of `num10` is divisible by 15
// and if none of these conditions are satisfied, then assign the value of
// `num10` to `answer10`
function exercise10(num10) {
let answer10;
// --------------------------------------------
// Write your code for the exercise below here:
// --------------------------------------------
if (num10 % 15 === 0){
answer10 ="Fizz Buzz";
} else if (num10 % 5 === 0){
answer10 = "Bizz";
} else if (num10 % 3 === 0){
answer10 = "Fizz";
} else {
answer10 = num10;
}
// --------------------------------------------
// And above here
// --------------------------------------------
return answer10;
}
// Congrats, you made it to the end! You rock!
// Did you find this easy or hard? If you used references, which ones helped you?
// Please answer in a comment below.
//
// Email your file to us or commit your file to GitHub and email us a link.