-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
452 lines (367 loc) · 12.6 KB
/
script.js
File metadata and controls
452 lines (367 loc) · 12.6 KB
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
444
445
446
447
448
449
450
451
452
function intro(){
var name1, age, school, id;
name1 = prompt("Enter your name: ");
age = prompt("Enter age: ");
school = prompt("Enter school: ");
id = prompt("Enter schoool id: ")
console.log('Student details: Name: ' + name1 + ' Age: ' + age + ' school: ' + school + ' ID ' + id);
alert(name1 + ', thanks for your feedback.');
//typeof operator
//operator precedence
let now = 2021;
let birthYear = 1998;
let fullAge = 18;
var isFullAge = now - birthYear >= fullAge;
console.log(isFullAge);
}
// intro();
/////CODING CHALLENGE 1///////////////////
function codingChallenge1(){
var mark_BMI, mark_mass, mark_height;
var john_BMI, john_mass, john_height = 150;
mark_BMI = mark_mass / (mark_height^2);
john_BMI = john_mass / (mark_height^2);
console.log('Mark\'s BMI: ' + mark_BMI + '\nJohn\'s BMI: ' + john_BMI);
if(mark_BMI > john_BMI){
console.log('Mark has a larger BMI');
}else {
console.log('John has a larger BMI')
}
//USING TERNARY
mark_BMI > john_BMI ? console.log('Defined: Mark has a larger BMI') : console.log('Undefined: John has a larger BMI');
// FALSY VALUEs
//undefined, null, 0, NaN, '' -- Not exactly false but produce a false result when evaluated under a TRUE or FALSE condition
//=== is the strict comparison operator while == does type coersion
}
// codingChallenge1();
//CODING CHALLENGE 3//
function codingChallenge3(){
var j_game1 = 89, j_game2 = 120, j_game3 = 103;
var m_game1 = 116, m_game2 = 94, m_game3 = 123;
var r_game1 = 97, r_game2 = 94, r_game3 = 123;
var j_avg = (j_game1 + j_game2 + j_game3)/3;
var m_avg = (m_game1 + m_game2 + m_game3)/3;
var r_avg = (r_game1 + r_game2 + r_game3)/3;
// j_avg > m_avg ? console.log('John\'s teams won!') : console.log('Mark\'s teams won!');
if(j_avg > m_avg && j_avg > r_avg){
console.log('John\'s teams won!')
}else if(m_avg > j_avg && m_avg > r_avg){
console.log('Mark\'s teams won!')
}else if(r_avg > m_avg && r_avg > j_avg){
console.log('Mary\'s teams won!')
}
else if(j_avg === m_avg){
console.log('DRAW between John and Mark');
}else if(j_avg === r_avg){
console.log('DRAW between John and Mary');
}else if(m_avg === r_avg){
console.log('DRAW between Mark and Mary');
}
else {
console.log('INVALID DATA');
}
}
// codingChallenge3();
function functionTut(){
//Parameters -- Defined in the signature
//Arguments -- those passed into the function
var j_game1 = 89, j_game2 = 120, j_game3 = 103;
var m_game1 = 116, m_game2 = 94, m_game3 = 123;
var r_game1 = 97, r_game2 = 94, r_game3 = 123;
var j_avg = (j_game1 + j_game2 + j_game3)/3;
var m_avg = (m_game1 + m_game2 + m_game3)/3;
var r_avg = (r_game1 + r_game2 + r_game3)/3;
// j_avg > m_avg ? console.log('John\'s teams won!') : console.log('Mark\'s teams won!');
if(j_avg > m_avg && j_avg > r_avg){
return 'James';
}else if(m_avg > j_avg && m_avg > r_avg){
return 'Mark';
}else if(r_avg > m_avg && r_avg > j_avg){
return 'Mary';
}
else if(j_avg === m_avg){
draw_arr = [];
return draw_Arr['James', 'Mark']
}else if(j_avg === r_avg){
return draw_Arr['James', 'Mary']
}else if(m_avg === r_avg){
return draw_Arr['Mary', 'Mark']
}
else {
console.log('INVALID DATA');
}
}
// functionTut();
function logWinner(){
var winner = functionTut();
console.log(`${winner}\'s team won!`);
}
// logWinner();
function calculateAge(yob){
return 2021 - yob;
}
function yearsUntilRetirement(year, firstName){
var age = calculateAge(year);
var remainingYears = 65 - age;
if (remainingYears < 0){
console.log(`${firstName} is already retired`);
}else {
console.log(`${firstName} retires in ${remainingYears} years.`);
}
}
// yearsUntilRetirement(1989, 'Mike');
////////////FUNCTION DECLARATIONS////////////////////
//Can be called BEFORE they are initialized
// console.log(`${whatDoYouDo('teacher', 'Simon')}`);
function whatDoYouDo(job, firstName){
switch(job){
case 'teacher':
return `${firstName} teaches kids how to code`;
case 'designer':
return `${firstName} designs beautiful websites`;
case 'driver':
return `${firstName} drives ubers in NYC`;
default:
return `Invalid inputs`
}
}
/////////////FUNCTION EXPRESSIONS//////////////////
//Can only be called AFTER they are declared. Below the function
const whatDoYouDo2 = function(job, firstName){
switch(job){
case 'teacher':
return `${firstName} teaches kids how to code`;
case 'designer':
return `${firstName} designs beautiful websites`;
case 'driver':
return `${firstName} drives a cab in NYC`;
default:
return `Invalid inputs`
}
}
// console.log(`${whatDoYouDo2('designer', 'Ken')}`);
/////////////CODING CHALLENGE 3////////////////////
//A TIP CALCULATOR
function tipCal(){
let bills = [124, 48, 268];
function tipCalculator(bill) {
var tip;
if(bill < 50){
tip = 0.2;
}else if (bill >= 50 && bill <= 200){
tip = 0.15;
}else if (bill > 200){
tip = 0.1;
}else {
console.log('INVALID BILL');
}
return tip * bill;
}
let tipsArray = [tipCalculator(bills[0]), tipCalculator(bills[1]), tipCalculator(bills[2])];
let billsArray = [bills[0] + tipsArray[0], bills[1] + tipsArray[1], bills[2] + tipsArray[2]];
console.log(tipsArray, billsArray);
}
// tipCal();
///////////OBJECTS/////////////////////
function objectsFun(){
const student = {
firstName: 'Eric',
lastName: 'Philip',
status: 'Single',
jobs: ['Software Engineer', 'Content Strategist', 'Businessman'],
yearOfBirth: 1998,
isMarried: false,
calcAge: function(){ //Function expression
this.age = 2021 - this.yearOfBirth;
}
}
// student.firstName = 'Mike';
student.calcAge();
console.log(student)
// console.log(student.calcAge(student.yearOfBirth));
}
// objectsFun();
////////CODING CHALLENGE 4//////////////////
function codingChallenge4(){
let mark = {
fullName: 'Mark Sn',
mass: 57,
height: 157,
calcBMI: function(){
this.bmi = this.mass/(this.height**2);
return this.bmi;
}
}
mark.calcBMI();
let john = {
fullName: 'John Sn',
mass: 57,
height: 157,
calcBMI: function(){
this.bmi = this.mass/(this.height**2);
return this.bmi;
}
}
john.calcBMI();
if(mark.bmi > john.bmi){
console.log(`${mark.fullName} has a higher BMI than ${john.fullName}. John\'s BMI = ${john.bmi.toFixed(4)}`);
}else if(mark.bmi < john.bmi){
console.log(`${mark.fullName} has a higher BMI than ${john.fullName}. Marks\'s BMI = ${mark.bmi.toFixed(4)}`);
}else if(mark.bmi === john.bmi){
console.log(`${john.fullName} and ${mark.fullName} have equal BMIs of ${john.bmi.toFixed(4)}`)
} else {
console.log(`FAILED`);
}
}
////////CODING CHALLENGE 5//////////////
var johnObject = {
objectName: 'John',
bills: [124, 48, 268, 180, 42],
newBills: [],
tips: [],
calcTips: function(){
for(var i = 0; i < this.bills.length; i++){
var tip;
if(this.bills[i] < 50){
tip = 0.2;
}else if(this.bills[i] >= 50 && this.bills[i] < 200){
tip = 0.15;
}else{
tip = 0.1
}
this.tips.push(tip * this.bills[i]);
this.newBills.push(this.bills[i] + this.tips[i]);
}
}
}
johnObject.calcTips();
console.log(johnObject);
var markObject = {
objectName: 'Mark',
bills: [77, 375, 110, 45],
newBills: [],
tips: [],
calcTips: function(){
for(var i = 0; i < this.bills.length; i++){
var tip;
if(this.bills[i] < 50){
tip = 0.2;
}else if(this.bills[i] >= 50 && this.bills[i] < 200){
tip = 0.15;
}else{
tip = 0.1
}
this.tips.push(tip * this.bills[i]);
this.newBills.push(this.bills[i] + this.tips[i]);
}
}
}
markObject.calcTips();
console.log(markObject);
var avgTips = function(johnTips, markTips){
//JOHN
var johnTipsSum = 0;
for(var i = 0; i < johnTips.length; i++){
johnTipsSum += johnTips[i];
}
var johnAvgTips = johnTipsSum/johnTips.length;
//MARK
var markTipsSum = 0;
for(var i = 0; i < markTips.length; i++){
markTipsSum += markTips[i];
}
var markAvgTips = markTipsSum/markTips.length;
if(johnAvgTips > markAvgTips) {
console.log(`${johnObject.objectName} spent more on tips than ${markObject.objectName}`)
}else{
console.log(`${markObject.objectName} spent more on tips than ${johnObject.objectName}`)
}
}
avgTips(johnObject.tips, markObject.tips);
//Coding challenge 7//
(function(){
var Questions = function(qns, answers, correct){
this.qns = qns;
this.answers = answers;
this.correct = correct;
}
Questions.qns = [
'What\'s the name of this course tutor?',
'Who was the first president of Kenya?',
'Is Nairobi the capical city of Kenya?',
'What is the hearbeat rate of a normal human?',
'Between cat and dog, which pet is bigger?',
]
Questions.answers = [
['Jonas', 'Mike', 'Steve'],
['Kibaki', 'Uhuru', 'Jomo'],
[true, false],
[65, 80, 56, 72],
['Dog', 'Cat']
]
Questions.correct = [
Questions.answers[0][0],
Questions.answers[1][2],
Questions.answers[2][0],
Questions.answers[3][3],
Questions.answers[4][0]
]
var rightWrong = {
correct: 'Correct!',
wrong: 'Wrong!',
elseResponse: 'Invalid input!'
}
var randomNum = Math.floor(Math.random() * Questions.qns.length);
for(var i = 0; i < Questions.answers[randomNum].length; i++){
console.log(`${i}: ${Questions.answers[randomNum][i]}`);
}
//Index of the entered value
var playerAnswer = parseInt(prompt(Questions.qns[randomNum]));
//Actual answer
var actualAns = Questions.correct[randomNum];
//Array with answers for the current random question
var currentArrAns = Questions.answers[randomNum]
if(currentArrAns.indexOf(actualAns) === playerAnswer){
console.log(rightWrong.correct)
}else if(currentArrAns.indexOf(actualAns) !== playerAnswer){
console.log(rightWrong.wrong)
}else {
console.log(rightWrong.elseResponse);
}
})();
//CODING CHALLENGE 7: improved solution
(function(){
var Questions = function(qn, answers, correct){
this.qn = qn;
this.answers = answers;
this.correct = correct;
}
Questions.prototype.displayAnswers = function(){
console.log(this.qn);
for(var i = 0; i < this.answers.length; i++){
console.log(`${i}: ${this.answers[i]}`);
}
return this.correct;
}
var q1 = new Questions('What\'s the name of this course tutor?', ['Jonas', 'Mike', 'Steve'], 0);
var q2 = new Questions('Who was the first president of Kenya?', ['Kibaki', 'Uhuru', 'Jomo'], 2);
var q3 = new Questions('Is Nairobi the capical city of Kenya?', [true, false], 0);
var qnsArray = [q1, q2, q3];
var n = Math.floor(Math.random() * qnsArray.length);
var correctAns = qnsArray[n].displayAnswers();
Questions.prototype.displayQn = function(){
var userVal = parseInt(prompt(this.qn));
console.log(userVal)
return userVal;
}
var userVal = qnsArray[n].displayQn();
Questions.__proto__.rightWrong2 = {
correct1: 'Correct!',
wrong1: 'Wrong!'
}
if(userVal === correctAns){
console.log(Questions.rightWrong2.correct1)
}else {
console.log(Questions.rightWrong2.wrong2);
}
})();