-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
815 lines (631 loc) · 14.5 KB
/
script.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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
/**
* Variables and datatypes
*/
/*
var firstName = 'Subham';
console.log(firstName);
var lastName = 'Ghimire';
var age = 20;
var fullAge = true;
console.log(fullAge);
var job;
console.log(job);
job = 'Developer';
console.log(job);
//Variables naming rules
var _3years = 3;
var subhamSujan = 'subham and sujan';
var if = 20;
/**
* Variables mutation and type coercion
*/
/*
var firstName = 'subham';
var age = 20;
//Type coercion
console.log(firstName + ' '+ age);
var job, isMarried;
job = 'Developer';
isMarried = 'false';
console.log(firstName+ ' is '+ age+ ' years old '+job+
'. Is he married? '+ isMarried);
//Variable mutation
age = 'twenty';
job = 'Driver';
alert(firstName+ ' is '+ age+ ' years old '+job+
'. Is he married? '+ isMarried);
var lastName = prompt('What is his last name?:)');
console.log(firstName + ' '+lastName);
*/
/**
* Basic Operators
*/
/*
var year, yearSubham, yearSujan;
now = 2019;
ageSubham = 20;
ageSujan = 23;
//Math Operators
yearSubham = now - 20;
yearSujan = now - 23;
console.log(yearSubham);
console.log(now + 2);
console.log(now * 2);
console.log(now / 2);
//Logical operators
var subhamOlder = ageSubham < ageSujan;
console.log(subhamOlder);
//typeof operator
console.log(typeof subhamOlder);
console.log(typeof ageSubham);
console.log(typeof 'sujan is older than subham');
var x;
console.log(typeof x);
*/
/**
* Operator precedence
*/
/**
var now = 2019;
var yearSubham = 1999;
var fullAge = 18;
//Multiple operators
var isFullAge = now - yearSubham >= fullAge; //true
console.log(isFullAge);
var ageSubham = now - yearSubham;
var ageSujan = 23;
var average = (ageSubham + ageSujan) / 2;
console.log(average);
//Multiple assignments
var x, y;
x = y = (3 + 5) * 4 - 6;// 8 * 4 - 6// 32 - 6// 26
console.log(x, y);
// More operators
x *=2
console.log(x);
x += 10;
console.log(x);
x++;
console.log(x);
x--;
console.log(x);
*/
/*
var massSubham, massSujan;
massSubham = 60;//kg
massSujan = 66;//kg
var heightSubham, heightSujan;
heightSubham = 1.66;//meters
heightSujan = 1.70;//meters
var bmiSubham = massSubham / (heightSubham * heightSubham);
var bmiSujan = massSujan / (heightSujan * heightSujan);
console.log(bmiSubham, bmiSujan);
var sujanHigher = bmiSujan > bmiSubham;
console.log('Is sujan\'s bmi higher than subham\'s bmi ? '+ sujanHigher);
*/
/**
* IF / else statements
*/
/*
var firstName = 'Subham';
var civilStatus = 'single';
if (civilStatus == 'married') {
console.log(firstName+ ' is married');
} else {
console.log(firstName + ' will hopefully marry soon:)');
}
var isMarried = true;
if (isMarried) {
console.log(firstName+ ' is married');
} else {
console.log(firstName + ' will hopefully marry soon:)');
}
var massSubham, massSujan;
massSubham = 60;//kg
massSujan = 66;//kg
var heightSubham, heightSujan;
heightSubham = 1.66;//meters
heightSujan = 1.70;//meters
var bmiSubham = massSubham / (heightSubham * heightSubham);
var bmiSujan = massSujan / (heightSujan * heightSujan);
if(bmiSujan > bmiSubham) {
console.log('Sujan\'s Bmi is higher than Subham\'s');
} else {
console.log('Subham\'s Bmi is higher than Sujan\'s');
}
*/
/**
* Boolean logic
*/
/*
var firstName = 'Subham';
var age = 18;
if (age < 13) {
console.log(firstName + ' is a boy.');
} else if(age >= 13 && age < 20 ) {
console.log(firstName + ' is a teenager');
}else {
console.log(firstName + ' is a man.');
}
*/
/**
* The Ternary operator and switch statements
*/
/*
var firstName = 'Subham';
var age = 18;
//ternary operator
age >= 18 ? console.log(firstName + ' drinks beer') //condition(>=),if(?),else(:)
:console.log(firstName + ' drinks juice');
var drink = age >= 18 ? 'beer' : 'juice';
console.log(drink);
//if else in this case
if (age >= 18) {
var drink = 'beer';
} else {
var drink = 'juice';
}
//Switch statements
var job = 'developer';
switch(job) {
case 'teacher':
console.log(firstName+' teaches how to code');
break;
case 'developer':
console.log(firstName+' does web developing');
break;
case 'driver':
console.log(firstName+ ' drives a taxi');
break;
default:
console.log(firstName+' does nothing');
}
var age = 16;
switch(true) {
case age < 13:
console.log(firstName + ' is a boy.');
break;
case age >= 13 && age < 20:
console.log(firstName + ' is a teenager');
break;
default:
console.log(firstName + ' is a man.');
}
*/
/**
* Truthy and Falsy values and equality operators
*/
/*
//falsy values: undefined, null, 0, '', NULL
//truthy values: Not falsy values
var height;
height = 20;
if (height || height === 0) {
console.log('Variable is defined');
}else {
console.log('Variable has not been defined');
}
//Equality operators
if (height === '20') {
console.log('The == operator does type coercion');
}else {
console.log('The === operator doesnot type coercion')
}
*/
/*
var avgJohn = (89 + 120 + 103) / 3;
var avgMike = (116 + 94 + 123) / 3;
/*
if (avgJohn > avgMike) {
console.log('John wins the match which is '+avgJohn);
} else if (avgJohn == avgMike) {
console.log('Match is draw');
}else {
console.log('Mike wins the match which is '+avgMike);
}
*/
/*
var avgMary = (97 + 134 + 105) / 3;
/*
avgMary > avgJohn && avgMike ? console.log('Mary wins the match which is '+avgMary)
: console.log('Mary can\'t make change');
*/
/*
console.log(avgJohn, avgMike, avgMary);
if (avgJohn > avgMike && avgJohn > avgMary) {
console.log('John wins the match which is '+avgJohn);
}else if (avgMike > avgJohn && avgMike > avgMary) {
console.log('Mike wins the match which is '+avgMike);
}else if(avgMary > avgJohn && avgMary > avgMike) {
console.log('Mary wins the match which is '+avgMary);
}else {
console.log('There is a draw');
}
*/
/**
* Functions
*/
/*
function calculateAge(birthYear) {
return 2019 - birthYear;
}
var ageSubham = calculateAge(1999);
var ageSubh = calculateAge(1998);
var ageSubhamGG = calculateAge(2001);
console.log(ageSubham, ageSubh, ageSubhamGG);
function yearsUntilrement(years, firstName)
{
var age = calculateAge(years);
var retirement = 65 - age;
if (retirement > 0){
console.log(firstName+' retires in '+retirement+' years.');
} else {
console.log(firstName+' is already retired.');
}
}
yearsUntilrement(1999,'Subham');
yearsUntilrement(1990,'Mike');
yearsUntilrement(1950,'John');
*/
/**
* Function statements and expressions
*/
/*
//Function declaration
//function whatUdo(job, firstName) {}
//Function expression
var whatUdo = function(job, firstName) {
switch(job) {
case 'teacher':
return firstName + ' teaches to code';
case 'developer':
return firstName + ' does web developing';
case 'designer':
return firstName + ' does designing job';
default:
return firstName + ' does nothing';
}
}
console.log(whatUdo('teacher','John'));
console.log(whatUdo('retired','Mark'));
*/
/**
* Arrays
*/
/*
//Initialize new array
var names = ['Subham','Mike','John'];
var years = new Array(1999,1998,1978);
console.log(names[0]);
console.log(names.length);
//Mutate array data
names[1] = 'Mary';
names[names.length] = 'Subh';
console.log(names);
//Different data types
var subhamInfo = ['Subham','Ghimire',1999,'developer',false];
subhamInfo.push('red');
subhamInfo.unshift('Mr.');
console.log(subhamInfo);
subhamInfo.pop()
subhamInfo.pop()
subhamInfo.shift()
console.log(subhamInfo);
console.log(subhamInfo.indexOf(1999));
console.log(subhamInfo.indexOf(19));
let isDesigner = subhamInfo.indexOf('designer') == -1 ? 'Subham is not a designer'
: ' Subham is a designer';
console.log(isDesigner);
*/
/*
function tipCalculator(bill)
{
var percentage;
if (bill < 50){
percentage = 0.2;
} else if (bill >= 50 && bill < 200) {
percentage = 0.15;
} else {
percentage = 0.1;
}
return percentage * bill;
}
var bills = [124, 48, 268];
var tips = [tipCalculator(bills[0]),
tipCalculator(bills[1]),
tipCalculator(bills[2])];
var finalAmount = [bills[0]+tips[0],
bills[1]+tips[1],
bills[2]+tips[2]];
console.log(tips);
console.log(finalAmount);
*/
/**
* Objects and properties
*/
/*
//object literal
var subham = {
firstName : 'Subham',
lastName : 'Ghimire',
birthYear : 1999,
family : ['James','Mark','bob','Mary'],
job : 'developer',
isMarried : false
};
console.log(subham);
console.log(subham.firstName);
console.log(subham['lastName']);
var x = 'birthYear';
console.log(subham[x]);
subham.job = 'designer';
subham['isMarried'] = true;
console.log(subham);
//new object syntax
var shekhar = new Object();
shekhar.firstName = 'Shekhar';
shekhar.birthYear = 2001;
shekhar['lastName'] = 'Ghimire';
console.log(shekhar);
*/
/**
* Objects and methods
*/
/*
var subham = {
firstName : 'Subham',
lastName : 'Ghimire',
birthYear : 1999,
friends : ['James','Mark','bob','Mary'],
job : 'developer',
isMarried : false,
calcAge : function() {
this.age = 2019 - this.birthYear;
}
};
// var age = console.log(subham.calcAge());
subham.calcAge()
console.log(subham);
*/
/*
var Mark = {
fullName : 'Mark miller',
mass : 50,
height : 1.57,
calcBmi : function() {
this.bmi = this.mass/(this.height*this.height);
return this.bmi
}
};
var John = {
fullName : 'John smith',
mass : 58,
height : 1.66,
calcBmi : function() {
this.bmi = this.mass/(this.height*this.height);
return this.bmi
}
};
// Mark.calcBmi();
// John.calcBmi();
// console.log(John,Mark);
if (Mark.calcBmi() > John.calcBmi()) {
console.log(Mark.fullName+' has higher than John bmi = '+Mark.bmi);
} else if(Mark.bmi < John.bmi) {
console.log(John.fullName+' has higher than Mark bmi = '+John.bmi);
} else {
console.log('They have same Bmi');
}
*/
/**
* Loops and iterations
*/
/*
//For loop
for (var i = 0; i <= 20; i+=2) {
console.log(i);
}
var subhamInfo = ['Subham','Ghimire',1999,'developer',false];
for (var i = 0; i < subhamInfo.length; i++) {
console.log(subhamInfo[i]);
}
//While loop
var i = 0;
while(i < subhamInfo.length) {
console.log(subhamInfo[i]);
i++;
}
*/
/*
//continue and break statements
var subhamInfo = ['Subham','Ghimire',1999,'developer',false];
for (var i = 0; i < subhamInfo.length; i++) {
if (typeof subhamInfo[i] !== 'string' )
continue;
console.log(subhamInfo[i]);
}
for (var i = 0; i < subhamInfo.length; i++) {
if (typeof subhamInfo[i] !== 'string' )
break;
console.log(subhamInfo[i]);
}
//looping backwards
for (var i = subhamInfo.length - 1; i >=0; i--) {
console.log(subhamInfo[i]);
}
*/
/*
var John = {
firstName : 'John',
bills : [124, 48, 268, 180, 42],
calcTips : function() {
this.tips = [];
this.finalValues = [];
for ( var i = 0; i < this.bills.length; i++)
{
//Determine % based on tiping rules
var percentage;
var bill = this.bills[i]
if (bill < 50){
percentage = 0.2;
} else if (bill>= 50 && bill < 200) {
percentage = 0.15;
} else {
percentage = 0.1;
}
//Add results
this.tips[i] = bill * percentage;
this.finalValues[i] = bill + bill * percentage;
}
}
}
var Mark = {
firstName : 'Mark',
bills : [77, 475, 110, 45],
calcTips : function() {
this.tips = [];
this.finalValues = [];
for ( var i = 0; i < this.bills.length; i++)
{
//Determine % based on tiping rules
var percentage;
var bill = this.bills[i]
if (bill < 100){
percentage = 0.2;
} else if (bill>= 100 && bill < 300) {
percentage = 0.1;
} else {
percentage = 0.25;
}
//Add results
this.tips[i] = bill * percentage;
this.finalValues[i] = bill + bill * percentage;
}
}
}
function calcAvg(tips) {
var sum = 0;
for (var i = 0; i < tips.length; i++) {
sum += tips[i];
}
return sum / tips.length;
}
//do calculations
John.calcTips();
Mark.calcTips();
John.average = calcAvg(John.tips);
Mark.average = calcAvg(Mark.tips);
console.log(John, Mark);
if(John.average > Mark.average) {
console.log(John.firstName+' pays higher tips than Mark');
} else if(Mark.average > John.average) {
console.log(Mark.firstName+' pays higher tips than John');
} else {
console.log('Both pays same tips')
}
*/
/*
*Calculate Factorial
function factorial(n){
return (n != 1) ? n * factorial(n-1) : 1;
}
console.log(factorial(5))
*/
/* Even or Odd in an Array
let a = [22,21,3,2,1,4,8,10]
function parray(a){
for (let i = 0;i<=(a.length-1);i++){
if (a[i]%2==0) {
console.log("It is even")
} else {
console.log("It is odd")
}
}
}
parray(a)
*/
/**
*
* Hoisting
*/
/*
//Functions
calculateAge(1999)
function calculateAge(year){
console.log(2019 - year)
}
// calculateAge(1999)
// retirement(1998)
var retirement = function(year){
console.log(65 - (2019 - year))
}
// retirement(1998)
//Variables
console.log(age);
var age = 23;
function foo(){
// console.log(age);
var age = 25;
console.log(age);
}
foo();
console.log(age)
*
*/
/*
* Scoping
*/
/*
var a = 'Hello'
first()
function first(){
var b = 'Hi'
second();
function second(){
var c = 'Hey'
third()
}
}
//third function cannot access first & second function
//it is outside of the scope and only access global variable
function third() {
var d = ' Subham'
//console.log(c)
console.log(a + d)
}
*/
/**
* This keyword
*/
/*
//console.log(this)
calculateAge(1998)
function calculateAge(year){
console.log(2019 - year)
console.log(this)
}
*/
/**
var subham = {
name : 'subham',
yearOfBirth: 1999,
calculateAge: function(){
console.log(this)
console.log(2019 - this.yearOfBirth)
//regular function and hence this keyword points to window object
/* function innerFunction() {
console.log(this)
}
innerFunction();
}
}
/*
subham.calculateAge()
var shekhar = {
name: 'shekhar',
yearOfBirth: 1998,
}
//method borrowing
shekhar.calculateAge = subham.calculateAge;
shekhar.calculateAge();
*/