-
Notifications
You must be signed in to change notification settings - Fork 0
/
w1d2.js
445 lines (372 loc) · 10.1 KB
/
w1d2.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
/*
Write a function sameLength that accepts two strings as arguments,
and returns true if those strings have the same length, and false otherwise.
*/
function sameLength (string1, string2) {
if (string1.length === string2.length) {
return true;
} else {
return false;
}
}
sameLength("hello", "hi");
// => false
sameLength("yo", "hi");
// => true
/*
Write a function passwordLongEnough that accepts a "password" as a parameter and returns true if that password is long enough -- you get to decide what constitutes long enough.
*/
function passwordLongEnough (password) {
if (password.length>=7) {
return true;
} else {
return false;
}
}
passwordLongEnough("secretword");
//=> true
passwordLongEnough("secret");
//=> false
/*
Conditionals: if
Write a function bouncer that accepts a person's name and age as arguments, and returns either "Go home, NAME.", or "Welcome, NAME!"
(where NAME is the parameter that represents the person's name) depending on whether or not the person is old enough to drink.
*/
function bouncer (name, age) {
if (age >= 21) {
return "Welcome, " + name;
}
else {
return "Go home, " + name;
}
}
bouncer("Jordan", 18);
//=> 'Go home, Jordan'
/*
Write a function max that takes two numbers as arguments, and returns the larger one.
*/
function max (num1, num2) {
if (num1 > num2) {
return num1;
} else if (num2 > num1) {
return num2;
} else {
return "numbers are equal";
}
}
max(82910,2);
//=> 82910
max(47,47);
//=> 'numbers are equal'
/*
Write a function min that takes two numbers as arguments, and returns the smaller one.
*/
function min (num1, num2) {
if (num1 < num2) {
return num1;
} else if (num2 < num1) {
return num2;
} else {
return "numbers are equal";
}
}
min(82910,2);
//=> 2
min(47,47);
//=> 'numbers are equal'
/*
Write functions larger and smaller that each accept two strings as arguments, and return the larger and smaller strings, respectively.
*/
function larger (string1, string2) {
if (string1.length > string2.length) {
return string1;
} else if (string2.length > string1.length) {
return string2;
} else {
return "The Strings are equal";
}
}
larger("I love Soylent", "Soylent loves me");
//=> 'Soylent loves me'
function smaller (string1, string2) {
if (string1.length < string2.length) {
return string1;
} else if (string2.length < string1.length) {
return string2;
} else {
return "The Strings are equal";
}
}
smaller("I love Soylent", "Soylent loves me");
//=> 'I love Soylent'
/*
Write the following functions that each accept a single number as an argument:
even: returns true if its argument is even, and false otherwise.
odd: the opposite of the above.
positive: returns true if its argument is positive, and false otherwise.
negative: the opposite of the above.
*/
function even (number) {
if (number % 2 === 0) {
return true;
} else {
return false;
}
}
even(237980);
//=> true
function odd (number) {
if (number % 2 === 1) {
return true;
} else {
return false;
}
}
odd(2390);
//=> false
function negative (number) {
if (number < 0) {
return true;
} else {
return false;
}
}
negative(-2190);
//=> true
function positive (number) {
if (number >= 0) {
return true;
} else {
return false;
}
}
positive(-2323);
//=> false
/*
Math
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math
Math.random()
Returns a pseudo-random number between 0 and 1.
Math.floor(x)
Returns the largest integer less than or equal to a number.
Math.ceil(x)
Returns the smallest integer greater than or equal to a number.
*/
/*
randInt: Should accept a single numeric argument (n), and return a number from 0 to n.
guessMyNumber: Should accept a single numeric argument and compare it to a random number between 0 and 5.
It should return one of the following strings:
"You guessed my number!" if the argument matches the random number.
"Nope! That wasn't it!" if the argument did not match the random number.
*/
function randInt (n) {
return Math.floor((Math.random()*n) + 1);
}
function guessMyNumber (num) {
if (num === Math.floor((Math.random()*5)) + 1) {
return "You guessed my number!";
} else {
return "Nope! That wasn't it!";
}
}
guessMyNumber(3);
//=> 'You guessed my number!'
/*
Conditionals: else if & else
This guy named "Joe" keeps blacking out at the bar that your function, bouncer (from the previous module),
is in charge of; thus, management has decided to add him to the "blacklist" -- modify the bouncer function from the previous section so that the person named "Joe" is rejected with an appropriate message, regardless of his age.
*/
function bouncer (name, age) {
if (age >= 21 && name != "Joe") {
return "Welcome, " + name;
} else if (name === "Joe") {
return "Go Home, Joe!!";
} else {
return "Go home, " + name;
}
}
bouncer("Joe",22);
//=> 'Go Home, Joe!!'
/*
Write a function called scoreToGrade that accepts a number as a parameter and returns a string
representing a letter grade corresponding to that score.
For example, the following grades should be returned given these scores:
'A' > 90
'B' >= 80
'C' >= 70
'D' >= 60
'F' < 60
function scoreToGrade(score) {
// TODO: your code here
}
scoreToGrade(95); // => 'A'
scoreToGrade(72); // => 'C'
*/
function scoreToGrade (number) {
if(number > 90) {
return "A";
} else if(number >= 80) {
return "B";
} else if(number >=70) {
return "C";
} else if(number >=60) {
return "D";
} else {
return "F";
}
}
scoreToGrade(99);
//=> 'A'
/*
Modify the scoreToGrade function so that it returns 'INVALID SCORE' if the score is greater than 100 or less than 0.
*/
function scoreToGrade (number) {
if(number > 100 || number < 0) {
return "INVALID SCORE";
} else if(number >= 90) {
return "A";
} else if(number >= 80) {
return "B";
} else if(number >=70) {
return "C";
} else if(number >=60) {
return "D";
} else {
return "F";
}
}
scoreToGrade(128);
//=> 'INVALID SCORE'
/*
Modify the scoreToGrade function so that it returns 'A+/A-' for scores of 98-100/90-92 respectively.
Apply the same logic for all other letter grades.
*/
function scoreToGrade (number) {
if(number > 100 || number < 0) {
return "INVALID SCORE";
} else if(number >= 93 && number <= 97) {
return "A";
} else if(number >= 90 && number <= 92) {
return "A-";
} else if(number >= 98 && number <= 100) {
return "A+";
} else if(number >= 83 && number <= 87) {
return "B";
} else if(number >= 80 && number <= 82) {
return "B-";
} else if(number >= 88 && number <= 89) {
return "B+";
} else if(number >= 78 && number <= 79) {
return "C+";
} else if(number >= 73 && number <= 77) {
return "C";
} else if(number >= 70 && number <= 72) {
return "C-";
} else if(number >= 68 && number <= 69) {
return "D+";
} else if(number >= 63 && number <= 67) {
return "D";
} else if(number >= 60 && number <= 62) {
return "D-";
} else if(number < 60 && number >= 0){
return "F";
}
}
scoreToGrade(82);
//=> 'B-'
scoreToGrade(62);
//=> 'D-'
/*
The guessMyNumber function from the Booleans & Conditionals module (More Practice section)
accepts a guess n and checks it against a random number from 0 to 5 -- if the guess n is greater than 5,
output a different message indicating that the guess is out of bounds.
NOTE: It will be helpful to first write a randInt function that accepts a number n and computes a random integer from 0 to n; then, you can use this function in guessMyNumber.
*/
function randInt (n) {
return Math.floor((Math.random()*n) + 1);
}
function guessMyNumber (num) {
if (num === Math.floor((Math.random()*5) + 1)) {
return "You guessed my number!";
} else {
return "Nope! That wasn\'\t it!";
}
}
guessMyNumber(3);
//=> 'You guessed my number!'
/*
The bar that employs our bouncer function has decided to do live music on Friday and Saturday nights, and will be admitting those that are over 18 to the bar on those nights; the catch however, is that all who are 21 or older will need to be given a wristband to distinguish them from the minors. Modify your bouncer function to handle this situation.
*/
function bouncer (age) {
if (age < 18) {
return "Too young to enter";
} else if(age >= 18 && age <= 21) {
return "Enter but no wristband for you";
} else if(age >= 21) {
return "Welcome, take a wristband";
}
}
bouncer(19);
//=> 'Enter but no wristband for you'
bouncer(42);
//=> 'Welcome, take a wristband'
/*
You should have noticed a large amount of repetitive code when modifying scoreToGrade to accommodate + or - grades. When we do lots of repetitive things, that's a clear signal that there's a better way. Write a helper function letterGrade that accepts two arguments, letter and score, and works as follows:
function letterGrade(letter, score) {
// your code here
}
// These are examples of what a *working* function would output.
letterGrade('A', 95); // => 'A'
letterGrade('A', 91); // => 'A-'
letterGrade('B', 88); // => 'B+'
letterGrade('monkey', 160); // => 'monkey-'
*/
function letterGrade(letter, score) {
if(score % 10 >= 8){
return letter + "+";
} else if(letter % 10 <= 2) {
return letter + "-";
} else if(letter === "F"){
return letter;
} else {
return letter;
}
}
letterGrade("F", 20);
//=> 'F'
letterGrade("B", 89);
//=> 'B+'
/*
Finally, use letterGrade to remove the repetition in scoreToGrade.
*/
function scoreToGrade(score){
if(score > 100 || score < 0){
return "Invalid Score";
}
else if(score > 90){
return letterGrade("A", score);
}
else if(score >=80){
return letterGrade("B", score);
}
else if(score >=70){
return letterGrade("C", score);
}
else if(score >=60){
return letterGrade("D", score);
}
else{
return "F";
}
}
scoreToGrade(71);
//=> 'C'
/*
It turns out that we can write logical and and logical or in terms of each other and logical not using De Morgan's Laws.
*/
/*
Write a function or that works like ||, but only uses ! and &&.
*/
/*
Write a function and that works like &&, but only uses ! and ||.
*/