-
Notifications
You must be signed in to change notification settings - Fork 192
/
Copy pathindex.js
2809 lines (1570 loc) · 61.3 KB
/
index.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
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// const chalk = require("chalk");
/**** Section 1👉 we need to do it in console ****/
// alert("Welcome, to Complete JavaScript course");
// console.log('Welcome, to complete JavaScript Course');
/**** Section 2👉 Code Editor for writing JS ****/
/**** Section 3👉 values and variables in JavaScript ****/
// var myName = 'vinod bahadur thapa';
// var myAge = 26;
// console.log(myage);
// Naming Practice
// var _myName = "vinod";
// var 1myName = "thapa";
// var _1my__Name = "bahadur";
// var $myName = "thapa technical";
// var myNaem% = "thapa technical";
// console.log(myNaem%);
// // **********************************************************************
// // 👉 // 🤩 SUBSCRIBE TO THAPA TECHNICAL YOUTUBE CHANNEL 🤩
// 👉 // 🤩 https://www.youtube.com/channel/UCwfaAHy4zQUb2APNOGXUCCA
// // **********************************************************************
/**** Section 4👉 Data Types in JavaScript ****/
// var myName = "vinod thapa";
// console.log(myName);
// var myAge = 26;
// console.log(myAge);
// var iAmThapas = false;
// console.log(iAmThapas);
// // typeof operator
// console.log(typeof(iAmThapas));
// DataTypes Practice
// console.log( 10 + "20");
// 9 - "5"
// console.log( 9 - "5"); //bug
// "Java" + "Script"
// console.log( "Java "+ "Script");
// " " + " "
// console.log( " " + 0);
// " " + 0
// "vinod" - "thapa"
// true + true
// true + false
// false + true
// false - true
// console.log("vinod" - "thapa");
// 🙋👨🏫 Interview Question 1 🙋👨🏫
// Difference between null vs undefined?
// var iAmUseless = null;
// console.log(iAmUseless);
// console.log(typeof(iAmUseless));
// //2nd javascript bug
// var iAmStandBy;
// console.log(iAmStandBy);
// console.log(typeof(iAmStandBy));
// 🙋👨🏫 Interview Question 2 🙋👨🏫
// What is NaN?
// NaN is a property of the global object.
// In other words, it is a variable in global scope.
// The initial value of NaN is Not-A-Number
// var myPhoneNumber = 9876543210;
// var myName = "thapa technical";
// console.log(isNaN(myPhoneNumber));
// console.log(isNaN(myName));
// if(isNaN(myName)){
// console.log("plz enter valid phone no");
// }
// NaN Practice 🤯
// NaN === NaN;
// Number.NaN === NaN;
// isNaN(NaN);
// isNaN(Number.NaN);
// Number.isNaN(NaN);
// console.log(Number.isNaN(NaN));
// 🙋👨🏫 Interview Question 1 🙋👨🏫
// var vs let vs const
/**** Section 5👉 Arithmetic operators in JavaScript ****/
// console.log(5+20);
// 1️⃣Assignment operators
// An assignment operator assigns a value to its left operand
// based on the value of its right operand.
// The simple assignment operator is equal (=)
// var x = 5;
// var y = 5;
// console.log("is both the x and y are equal or not" + x == y );
// I will tell you when we will see es6
// console.log(`Is both the x and y are equal : ${x == y}`);
// 2️⃣Arithmetic operators
// An arithmetic operator takes numerical values
// (either literals or variables) as their operands and
// returns a single numerical value.
// console.log(3+3);
// console.log(10-5);
// console.log(20/5);
// console.log(5*6);
// console.log("Remainder Operator " + 27%4);
// 🙄Increment and Decrement operator
// Operator: x++ or ++x or x-- or --x
// If used postfix, with operator after operand (for example, x++),
// the increment operator increments and returns the value before incrementing.
// var num = 15;
// var newNum = num-- + 5;
// console.log(num);
// console.log(newNum);
// Postfix increment operator means the expression is evaluated
// first using the original value of the variable and then the
// variable is incremented(increased).
// If used prefix, with operator before operand (for example, ++x),
// the increment operator increments and returns the value after incrementing.
// var num = 15;
// var newNum = --num + 5;
// console.log(num);
// console.log(newNum);
// Prefix increment operator means the variable is incremented first and then
// the expression is evaluated using the new value of the variable.
// 3️⃣Comparison operators
// A comparison operator compares its operands and
// returns a logical value based on whether the comparison is true.
// var a = 30;
// var b = 10;
// Equal (==)
// console.log(a == b);
// Not equal (!=)
// console.log(a != b);
// // Greater than (>)
// console.log(a > b);
// // Greater than or equal (>=)
// console.log(a >= b);
// // Less than (<)
// console.log(a < b);
// // Less than or equal (<=)
// console.log(a <= b);
// 4️⃣ Logical operators
// Logical operators are typically used with Boolean (logical) values;
// when they are, they return a Boolean value.
// var a = 30;
// var b = -20;
// Logical AND (&&)
// The logical AND (&&) operator (logical conjunction) for a set of
// operands is true if and only if all of its operands are true.
// console.log(a > b && b > -50 && b < 0);
// Logical OR (||)
// The logical OR (||) operator (logical disjunction) for a set of
// operands is true if and only if one or more of its operands is true.
// console.log((a < b) || (b > 0) || (b > 0));
// Logical NOT (!)
// The logical NOT (!) operator (logical complement, negation)
// takes truth to falsity and vice versa.
// console.log(!((a>0) || (b<0)));
// console.log(!true);
// // **********************************************************************
// // 👉 // 🤩 SUBSCRIBE TO THAPA TECHNICAL YOUTUBE CHANNEL 🤩
// 👉 // 🤩 https://www.youtube.com/channel/UCwfaAHy4zQUb2APNOGXUCCA
// // **********************************************************************
// 5️⃣ String Concatenation(operators)
// The concatenation operator (+) concatenates two string values together,
// returning another string that is the union of the two operand strings.
// console.log("Hello World");
// console.log("hello " + "world");
// var myName = "vinod";
// console.log(myName + " thapa");
// console.log(myName + " bahadur");
// console.log(myName + " bahadur Thapa");
// 😳 4 Challenge Time
// What will be the output of 3**3?
// What will be the output, when we add a number and a string?
// Write a program to swap two numbers?
// Write a program to swap two numbers without using third variable?
// sol 1: ✔
// console.log(9**2); // 9*9
// console.log(10 ** -1); 1/10
// sol 2: ✔
// console.log(5 + "thapa");
// sol 3: ✔
// var a = 5;
// var b = 10;
// output b=5; a=10
// var c = b; //c = 10
// b = a; // b = 5;
// a = c;
// console.log("the value of a is " + a);
// console.log("the value of b is " + b);
// sol 4: ✔
// var a = 5;
// var b = 10;
// // output b=5; a=10
// a = a + b; // a = 15
// b = a - b; // b = 5;
// a = a - b; // a = 10;
// console.log("the value of a is " + a);
// console.log("the value of b is " + b);
// 🙋👨🏫 Interview Question 4 🙋👨🏫
// What is the Difference between == vs === ?
// sol
// var num1 = 5;
// var num2 = '5';
// console.log(typeof(num1));
// console.log(typeof(num2));
// console.log(num1 == num2 );
// var num1 = 5;
// var num2 = '5';
// console.log(typeof(num1));
// console.log(typeof(num2));
// console.log(num2);
// console.log(num1 === num2 );
// ************************************************************
/**** Section 6👉 Control Statement -
*
* 1️⃣If...Else */
// The if statement executes a statement if a specified condition is truthy.
// If the condition is falsy, another statement can be executed.
// if raining = raincoat
// else no raincoat
// var tomr = 'sunny';
// if(tomr == 'rain'){
// console.log('take a raincoat');
// }else{
// console.log('No need to take a raincoat');
// }
// 🤩Challenge Time
// write a program that works out whether if a given year is a leap year or not?
// A normal year has 365 days, leap years have 366, with an extra day in February.
// var year = 2020;
// debugger;
// if(year % 4 === 0){
// if(year % 100 === 0){
// if(year % 400 === 0){
// console.log("The year " + year + " is a leap year");
// }else{
// console.log("The year " + year + " is not a leap year");
// }
// }else{
// console.log("The year " + year + " is a leap year");
// }
// }else{
// console.log("The year " + year + " is not a leap year");
// }
// What is truthy and falsy values in Javascript?
// we have total 5 falsy values in javascript
// 👉 0,"",undefined,null,NaN,false** is false anyway
// if(score = 5){
// console.log("OMG, we loss the game 😭");
// }else{
// console.log("Yay, We won the game 😀");
// }
// 2️⃣Conditional (ternary) operator
// The conditional (ternary) operator is the only JavaScript operator
// that takes three operands
// var age = 17;
// if(age >= 18){
// console.log("you are eligible to vote");
// }else{
// console.log("you are not eligible to vote");
// }
// var age = 18;
// console.log((age >= 18) ? "you can vote" : "you can't vote");
// 3️⃣ switch Statement
// Evaluates an expression, matching the expression's value to a
// case clause, and executes statements associated with that case.
// 1st without break statment
// Find the Area of circle, triangle and rectangle?
// var area = "square" ;
// var PI = 3.142, l=5, b=4, r=3;
// if(area == "circle"){
// console.log("the area of the circle is : " + PI*r**2);
// }else if(area == "triangle"){
// console.log("the area of the triangle is : " + (l*b)/2);
// }else if(area == "rectangle"){
// console.log("the area of the rectangle is : " + (l*b));
// }else{
// console.log("please enter valid data");
// }
// var area = "dsfsad" ;
// var PI = 3.142, l=5, b=4, r=3;
// switch(area){
// case 'circle':
// console.log("the area of the circle is : " + PI*r**2);
// break;
// case 'triangle':
// console.log("the area of the triangle is : " + (l*b)/2);
// break;
// case 'rectangle':
// console.log("the area of the rectangle is : " + (l*b));
// break;
// default:
// console.log("please enter valid data");
// }
// 🤗break
// Terminates the current loop, switch, or label
// statement and transfers
// program control to the statement following the terminated statement.
// 🤗continue
// Terminates execution of the statements in the current iteration of the
// current or labeled loop, and continues execution of the loop with the
// next iteration.
// 4️⃣ While Loop Statement
// The while statement creates a loop that executes a specified statement
// as long as the test condition evaluates to true.
// var num=20;
// // block scope
// while(num <= 10){
// console.log(num); //infinte loop
// num++;
// }
// 5️⃣ Do-While Loop Statement
// var num = 20;
// do{
// debugger;
// console.log(num); //infinte loop
// num++;
// }while(num <= 10);
// 6️⃣ For Loop
// for(var num = 0; num <= 10; num++){
// debugger;
// console.log(num);
// }
// 😀6: challenge Time 🏁
// JavaScript program to print table for given number (8)?
// output : 8 * 1 = 8
// 8 * 2 = 16(8*2)
// => 8 * 10 = 80
// for(var num = 1; num<= 10; num++){
// var tableOf = 12;
// console.log(tableOf + " * " + num + " = " + tableOf * num);
// }
// ************************************************************
/**** Section 5 👉 Functions in JavaScript ****/
// A JavaScript function is a block of code designed to perform a particular task.
// 1️⃣Function Definition
// Before we use a function, we need to define it.
// A function definition (also called a function declaration, or function statement)
// consists of the function keyword, followed by:
// The name of the function.
// A list of parameters to the function, enclosed in parentheses and separated by commas.
// The JavaScript statements that define the function, enclosed in curly brackets, {...}.
// var a = 10;
// var b = 20;
// var sum = a+b;
// console.log(sum);
// function sum(){
// var a = 10, b = 40;
// var total = a+b;
// console.log(total);
// }
// //
// 2️⃣Calling functions
// Defining a function does not execute it.
// A JavaScript function is executed when "something" invokes it (calls it).
// function sum(){
// var a = 10, b = 40;
// var total = a+b;
// console.log(total);
// }
// sum();
// 3️⃣ Function Parameter vs Function Arguments
// Function parameters are the names listed in the function's definition.
// Function arguments are the real values passed to the function.
// function sum(a,b){
// var total = a+b;
// console.log(total);
// }
// sum();
// sum(20,30);
// sum(50,50);
// sum(5,6)
// // **********************************************************************
// // 👉 // 🤩 SUBSCRIBE TO THAPA TECHNICAL YOUTUBE CHANNEL 🤩
// 👉 // 🤩 https://www.youtube.com/channel/UCwfaAHy4zQUb2APNOGXUCCA
// // **********************************************************************
// 🙋👨🏫 Interview Question 🙋👨🏫
// Why Functions?
// You can reuse code: Define the code once, and use it many times.
// You can use the same code many times with different arguments,
// to produce different results.
// OR
// A function is a group of reusable code which can be called anywhere
// in your program. This eliminates the need of writing the same code
// again and again.
// DRY => do not repeat yourself
// 4️⃣ Function expressions
// "Function expressions simply means
// create a function and put it into the variable "
// function sum(a,b){
// var total = a+b;
// console.log(total);
// }
// var funExp = sum(5,15);
// 5️⃣ Return Keyword
// When JavaScript reaches a return statement,
// the function will stop executing.
// Functions often compute a return value.
// The return value is "returned" back to the "caller"
// function sum(a,b){
// return total = a+b;
// }
// var funExp = sum(5,25);
// console.log('the sum of two no is ' + funExp );
// 6️⃣ Anonymous Function
// A function expression is similar to and has the same syntax
// as a function declaration One can define "named"
// function expressions (where the name of the expression might
// be used in the call stack for example)
// or "anonymous" function expressions.
// var funExp = function(a,b){
// return total = a+b;
// }
// var sum = funExp(15,15);
// var sum1 = funExp(20,15);
// console.log(sum > sum1 );
// ************************************************************
// 👻 Now It's Time for Modern JavaScript 😍😍
// 🙏🙏 Features of ECMAScript 2015 also known as ES6 🙏🙏
// 1️⃣ LET VS CONST vs VAR
// var myName = "thapa technical";
// console.log(myName);
// myName = "vinod thapa";
// console.log(myName);
// let myName = "thapa technical";
// console.log(myName);
// myName = "vinod thapa";
// console.log(myName);
// const myName = "thapa technical";
// console.log(myName);
// myName = "vinod thapa";
// console.log(myName);
// function biodata() {
// const myFirstName = "Vinod";
// console.log(myFirstName);
// if(true){
// const myLastName = "thapa";
// }
// // console.log('innerOuter ' + myLastName);
// }
// console.log(myFirstName);
// biodata();
// var => Function scope
// let and const => Block Scope
// 2️⃣ Template literals (Template strings)
// JavaScript program to print table for given number (8)?
// output : 8 * 1 = 8
// 8 * 2 = 16(8*2)
// => 8 * 10 = 80
// for(let num = 1; num<= 10; num++){
// let tableOf = 12;
// // console.log(tableOf + " * " + num + " = " + tableOf * num);
// console.log( ` ${tableOf} * ${num} = ${tableOf * num}` );
// }
// 3️⃣ Default Parameters
// Default function parameters allow named parameters to be
// initialized with default values if no value or undefined is passed.
// function mult(a,b=5){
// return a*b;
// }
// console.log(mult(3));
// 4️⃣ Destructuring in ES6
// The destructuring assignment syntax is a JavaScript expression
// that makes it possible to unpack values from arrays,
// or properties from objects, into distinct variables.
// ➡ Array Destructuring 🏁
// const myBioData = ['vinod', 'thapa', 26];
// let myFName = myBioData[0];
// let myLName = myBioData[1];
// let myAge = myBioData[2];
// let [myFName,myAge, myLName] = myBioData;
// console.log(myAge);
// we can add values too
// let [myFName,myLName,myAge, myDegree="MCS"] = myBioData;
// console.log(myDegree);
// ➡ Object destructuring 🏁
// const myBioData = {
// myFname : 'vinod',
// myLname : 'thapa',
// myAge : 26
// }
// let age = myBioData.age;
// let myFname = myBioData.myFname;
// let {myFname,myLname,myAge, myDegree="MCS"} = myBioData;
// console.log(myLname);
// 5️⃣ Object Properties
// ➡ we can now use Dynamic Properties
// let myName = "vinod";
// const myBio = {
// [myName] : "hello how are you?",
// [20 + 6] : "is my age"
// }
// console.log(myBio);
// ➡ no need to write key and value, if both are same
// let myName = "vinod thapa";
// let myAge = 26;
// const myBio = {myName,myAge}
// console.log(myBio);
// 6️⃣ Fat Arror Function
// 👻 Normal Way of writing Function
// console.log(sum());
// function sum() {
// let a = 5; b = 6;
// let sum = a+b;
// return `the sum of the two number is ${sum}`;
// }
// 👻 How to convert in into Fat Arrow Function
// const sum = () => `the sum of the two number is ${(a=5)+(b=6)}`;
// console.log(sum());
// 7️⃣Spread Operator
// const colors = ['red', 'green', 'blue', 'white', 'pink'];
// const myColors = ['red', 'green', 'blue', 'white','pink', 'yellow', 'black'];
// // // 2nd time add one more color on top and tell we need to write it again
// // // on myColor array too
// const MyFavColors = [ ...colors, 'yellow', 'black'];
// console.log(MyFavColors);
// ES7 features
// 1: array include
// const colors = ['red', 'green', 'blue', 'white', 'pink'];
// const isPresent = colors.includes('purple');
// console.log(isPresent);
// 2: **
// console.log(2**3);
// ES8 Features
// String padding
// Object.values()
// Object.entries()
// const message = "my name is vinod";
// console.log(message);
// console.log(message.padStart(5));
// console.log(message.padEnd(10));
// const person = { name: 'Fred', age: 87 };
// // // console.log( Object.values(person) );
// const arrObj = Object.entries(person);
// console.log(Object.fromEntries(arrObj));
// ES2018
// const person = { name: 'Fred', age: 87, degree : "mcs" };
// const sPerson = { ...person };
// console.log(person);
// console.log(sPerson);
// ES2019
// Array.prototype.{flat,flatMap}
// Object.fromEntries()
// ES2020
// #1: BigInt
// let oldNum = Number.MAX_SAFE_INTEGER;
// // console.log(oldNum);
// // console.log( 9007199254740991n + 12n );
// const newNum = 9007199254740991n + 12n;
// console.log(newNum);
// console.log(typeof newNum);
// const foo = null ?? 'default string';
// console.log(foo);
// ES2014
// "use strict";
// x = 3.14;
// console.log(x);
// ************************************************************
/**** Section 7👉 Arrays in JavaScript ****/
// When we use var, we can stored only one value at a time.
// var friend1 = 'ramesh';
// var friend2 = 'arjun';
// var friend3 = 'vishal';
// var myFriends = ['ramesh',22,male,'arjun',20,male,'vishal',true, 52];
// When we feel like storing multiple values in one variable then
// instead of var, we will use an Array.
// In JavaScript, we have an Array class, and
// arrays are the prototype of this class.
// example 🏁