-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.js
More file actions
1972 lines (1626 loc) · 40 KB
/
Copy pathjavascript.js
File metadata and controls
1972 lines (1626 loc) · 40 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
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
// console.log can print something on console
console.log("hello world");
"use strict";
// intro to variables
// variables can store some information
// we can use that information later
// we can change that information later
// declare a variable
var firstName = "Saurabh";
// use a variable
console.log(firstName);
// change value
firstName = "Shaurya";
console.log(firstName);
// rules for naming variables
// you cannot start with number
// example :-
// 1value (invalid)
// value1 (valid)
var value1 = 2;
console.log(value1);
// you can use only undersore _ or dollar symbol
// first_name (valid)
// _firstname (valid)
// first$name (valid)
// $firstname (valid)
// you cannot use spaces
// var first_name = "Saurabh"; // snake case writing
// var firstName = "Saurabh"; // camel case writing
// first name (invalid)
// convention
// start with small letter and use camelCase
// let keyword
// declare variable with let keyword
let firstName = "harshit";
firstName = "Mohit";
console.log(firstName);
// declare constants
const pi = 3.14;
console.log(pi);
// String indexing
let firstName = "saurabhmanipandey";
// h a r s h i t
// 0 1 2 3 4 5 6
// console.log(firstName[0]);
// length of string
// firstName.length
console.log(firstName.length);
console.log(firstName[firstName.length-2]);
// last Index : length - 1
// trim()
// toUpperCase()
// toLowerCase()
// slice()
let firstName = " Saurabh ";
// console.log(firstName.length);
// firstName = firstName.trim(); // "Saurabh12"
// console.log(firstName)
// console.log(firstName.length);
// firstName = firstName.toUpperCase();
// firstName = firstName.toLowerCase();
// console.log(firstName);
// start index
// end index
let newString = firstName.slice(1); // Saurabh
console.log(newString);
// typeof operator
// data types (primitive data types)
// string "saurabh"
// number 2, 4, 5.6
// booleans
// undefined
// null
// BigInt
// Symbol
// let age = 22;
// let firstName = "harshit";
// // console.log(typeof age);
// // 22 -> "22"
// // convert number to string.
// age = age + "";
// console.log(typeof(age)); "22"
// // convert string to number.
// let myStr = +"34";
// console.log(typeof myStr);
// let age = "18";
// age = Number(age);
// console.log(typeof age);
// string concatenation
let string1 = "17";
let string2 = "10";
let newString1 = +string1 + +string2;
console.log(typeof newString1);
// template string
let age = 24;
let firstName = "Shivansh"
// "my name is harshit and my age is 22 "
// let aboutMe = "my name is " + firstName + " and my age is " + age;
let aboutMe = `my name is ${firstName} and my age is ${age}`
console.log(aboutMe);
// undefined
// null
// let firstName;
// console.log(typeof firstName);
// firstName = "Pandey";
// console.log(typeof firstName, firstName);
// let myVariable = null;
// console.log(myVariable);
// myVariable = "Pandey";
// console.log(myVariable, typeof myVariable);
// console.log(typeof null);
// bug , error
// BigInt
// let myNumber = BigInt(12);
// let sameMyNumber = 123n;
// // console.log(myNumber);
// // console.log(Number.MAX_SAFE_INTEGER);
// console.log(myNumber+ sameMyNumber);
// booleans & comparison operator
// booleans
// true, false
// let num1 = 7;
// let num2 = "7";
// console.log(num1<num2);
// == vs ===
// console.log(num1 === num2);
// != vs !==
// console.log(num1 !== num2);
// truthy and falsy values
// falsy values
// false
// ""
// null
// undefined
// 0
// if else condition
// let age = 17;
// if(age>=18){
// console.log("User can play ddlc");
// }else {
// console.log("User can play mario");
// }
// let num = 13;
// if(num%2===0){
// console.log("even");
// }else{
// console.log("odd");
// }
// falsy values
// false
// ""
// null
// undefined
// 0
// truthy
// "abc"
// 1, -1
// let firstName= 0;
// if(firstName){
// console.log(firstName);
// }else{
// console.log("firstName is kinda empty");
// }
// ternary operator
// let age = 4;
// let drink;
// if(age>=5){
// drink = "coffee";
// }else{
// drink = "milk";
// }
// console.log(drink);
// ternary operator / conditional operator
// let age = 3;
// let drink = age >= 5 ? "coffee" : "milk";
// console.log(drink);
// and or operator
// if(firstName[0] === "H"){
// console.log("your name starts with H")
// }
// if(age > 18){
// console.log("you are above 18");
// }
// if(firstName[0] === "H" && age>18){
// console.log("Name starts with H and above 18");
// }else{
// console.log("inside else");
// }
let firstName = "arshit";
let age = 16;
if(firstName[0] === "H" || age>18){
console.log("inside if");
}else{
console.log("inside else");
}
// nested if else
// winning number 19
// 19 your guess is right
// 17 too low
// 20 too high
let winningNumber = 19;
let userGuess = +prompt("Guess a number");
if(userGuess === winningNumber){
console.log("Your guess is right!!");
}else{
if(userGuess < winningNumber){
console.log("too low !!!");
}else{
console.log("too high !!!");
}
}
// if
// else if
// else if
// else if
// else
// let tempInDegree = 50;
// if(tempInDegree < 0){
// console.log("extremely cold outside");
// }else if(tempInDegree < 16){
// console.log("It is cold outside");
// }else if(tempInDegree < 25){
// console.log("wheather is okay ");
// }else if(tempInDegree < 35){
// console.log("lets go for swim");
// }else if(tempInDegree < 45){
// console.log("turn on AC");
// }else{
// console.log("too hot!!");
// }
// console.log("hello");
// let tempInDegree = 50;
// if(tempInDegree < 0){
// console.log("extremely cold outside");
// }else if(tempInDegree < 16){
// console.log("It is cold outside");
// }else if(tempInDegree < 25){
// console.log("wheather is okay ");
// }else if(tempInDegree < 35){
// console.log("lets go for swim");
// }else if(tempInDegree < 45){
// console.log("turn on AC");
// }else{
// console.log("too hot!!");
// }
// console.log("hello there");
let tempInDegree = 4;
if(tempInDegree > 40){
console.log("too hot");
}else if(tempInDegree > 30){
console.log("lets go for swim");
}else if(tempInDegree > 20){
console.log("weather is cool");
}else if(tempInDegree > 10){
console.log("it is very cold outside");
}else{
console.log("extremely cold");
}
console.log("hello");
// switch statement
// let day = 7;
// if(day === 0){
// console.log("Sunday");
// }else if(day ===1){
// console.log("Monday");
// }else if(day ===2){
// console.log("Tuesday");
// }else if(day ===3){
// console.log("Wednesday");
// }else if(day ===4){
// console.log("Thrusday");
// }else if(day ===5){
// console.log("Friday");
// }else if(day ===6){
// console.log("Saturday");
// }else{
// console.log("Invalid Day");
// }
let day = 9;
switch(day){
case 0:
console.log("Sunday");
break;
case 1:
console.log("Monday");
break;
case 2:
console.log("Tuesday");
break;
case 3:
console.log("Wednesday");
break;
case 4:
console.log("Thrusday");
break;
case 5:
console.log("Friday");
break;
case 6:
console.log("Saturday");
break;
default:
console.log("Invalid Day");
}
// while loop
// 0 se 9
// dry don't repeat yourself
let i = 0; // 1 2 3 4
while(i<=9){
console.log(i);
i++;
}
console.log(`current value of i is ${i}`);
console.log("hello");
// while loop example
let num = 100;
// let total = 0; //1 + 2 +3
// let i = 0;
// while(i<=100){
// total = total + i;
// i++;
// }
// console.log(total);
// let total = (num*(num+1))/2;
// console.log(total);
// intro to for loop
// print 0 to 9
for(let i = 0;i<=9;i++){
console.log(i);
}
// console.log("value of i is ",i);
// for loop example
let total = 0;
let num = 100;
for(let i = 1; i<=num; i++){
total = total + i;
}
console.log(total);
// break keywork
// continue keyword
// for(let i = 1; i<=10; i++){
// if(i===4){
// break;
// }
// console.log(i);
// }
// for(let i = 1; i<=10; i++){
// if(i===4){
// continue;
// }
// console.log(i);
// }
console.log("hello there");
// do while loop
// while(i<=9){
// console.log(i);
// i++;
// }
// let i = 10;
// do{
// console.log(i);
// i++;
// }while(i<=9);
// console.log("value of i is ", i);
// intro to arrays
// reference type
// how to create arrays
// ordered collection of items
// let fruits = ["apple", "mango", "grapes"];
// let numbers = [1,2,3,4];
// let mixed = [1,2,2.3, "string", null, undefined];
// console.log(mixed);
// console.log(numbers);
// console.log(fruits[2]);
let fruits = ["apple", "mango", "grapes"];
let obj = {}; // object literal
// console.log(fruits);
// fruits[1] = "banana";
// console.log(fruits);
console.log(typeof fruits);
console.log(typeof obj);
console.log(Array.isArray(fruits));
console.log(Array.isArray(obj));
// array push pop
// array shift unshift
let fruits = ["apple", "mango", "grapes"];
console.log(fruits);
// push
// fruits.push("banana");
// console.log(fruits);
// pop
// let poppedFruit = fruits.pop();
// console.log(fruits);
// console.log("popped fruits is", poppedFruit);
// unshift
// fruits.unshift("banana");
// fruits.unshift("myfruit");
// console.log(fruits);
// shift
// let removedFruit = fruits.shift();
// console.log(fruits);
// console.log("removed fruits is ", removedFruit);
// primitve vs reference data types
// let num1 = 6;
// let num2 = num1;
// console.log("value is num1 is", num1);
// console.log("value is num2 is", num2);
// num1++;
// console.log("after incrementing num1")
// console.log("value is num1 is", num1);
// console.log("value is num2 is", num2);
// reference types
// array
let array1 = ["item1", "item2"];
let array2 = array1;
console.log("array1", array1);
console.log("array2", array2);
array1.push("item3");
console.log("after pushing element to array 1");
console.log("array1", array1);
console.log("array2", array2);
// how to clone array
// how to concatenate two arrays
let array1 = ["item1", "item2"];
// let array2 = ["item1", "item2"];
// let array2 = array1.slice(0).concat(["item3", "item4"]);
// let array2 = [].concat(array1,["item3", "item4"]);
// new way
// spread operator
let oneMoreArray = ["item3", "item4"]
let array2 = [...array1, ...oneMoreArray];
array1.push("item3");
console.log(array1===array2);
console.log(array1)
console.log(array2)
// for loop in array
let fruits = ["apple", "mango", "grapes", "banana"];
// for(let i=0; i<=9;i++){
// console.log(i);
// }
// console.log(fruits.length);
// console.log(fruits[fruits.length-2]);
let fruits2 = [];
for(let i=0; i < fruits.length; i++){
fruits2.push(fruits[i].toUpperCase());
}
console.log(fruits2);
// while loop in array
const fruits = ["apple", "mango", "grapes"];
const fruits2 = [];
let i = 0;
while(i<fruits.length){
fruits2.push(fruits[i].toUpperCase());
i++;
}
console.log(fruits2);
// for of loop in array
const fruits = ["apple", "mango", "grapes", "fruit4", "fruit5"];
const fruits2 = [];
// for(let fruit of fruits){
// fruits2.push(fruit.toUpperCase());
// }
// console.log(fruits2);
for(let i = 0; i<fruits.length; i++){
console.log(fruits[i]);
// for in loop in array
const fruits = ["apple", "mango", "grapes", "fruit4", "fruit5"];
const fruits2 = [];
for(let index in fruits){
fruits2.push(fruits[index].toUpperCase());
}
console.log(fruits2);
// array destructuring
const myArray = ["value1", "value2", "value3","value4"];
// let myvar1 = myArray[0];
// let myvar2 = myArray[1];
// console.log("value of myvar1", myvar1);
// console.log("value of myvar2", myvar2);
let [myvar1, myvar2, ...myNewArray] = myArray;
console.log("value of myvar1", myvar1);
console.log("value of myvar2", myvar2);
console.log(myNewArray);
// objects reference type
// arrays are good but not sufficient
// for real world data
// objects store key value pairs
// objects don't have index
// how to create objects
// const person = {name:"Golu ",age:22};
const person = {
firstName: "Vikram",
lastName: "Singh",
age: 22,
hobbies: ["guitar", "sleeping", "listening music"]
}
console.log(person);
// how to access data from objects
// console.log(person["firstName"]);
// console.log(person["age"]);
// console.log(person.hobbies);
// how to add key value pair to objects
person["person"] = "male";
console.log(person);
// difference between dot and bracket notaion
// const key = "email";
// const person = {
// name: "harshit",
// age: 22,
// "person hobbies": ["guitar", "sleeping", "listening music"]
// }
// console.log(person["person hobbies"]);
// person[key] = "harshitvashisth@gmail.com";
// console.log(person);
// how to iterate object
const person = {
name: "harshit",
age: 22,
"person hobbies": ["guitar", "sleeping", "listening music"]
}
// for in loop
// Object.keys
// for(let key in person){
// // console.log(`${key} : ${person[key]}`);
// console.log(key," : " ,person[key]);
// }
// console.log(typeof (Object.keys(person)));
// const val = Array.isArray((Object.keys(person)));
// console.log(val);
// for(let key of Object.keys(person)){
// console.log(person[key]);
// }
// computed properties
const key1 = "objkey1";
const key2 = "objkey2";
const value1 = "myvalue1";
const value2 = "myvalue2";
// const obj = {
// objkey1 : "myvalue1",
// objkey2 : "myvalue2",
// }
// const obj = {
// [key1] : value1,
// [key2] : value2
// }
const obj = {};
obj[key1] = value1;
obj[key2] = value2;
console.log(obj);
// spread operator
// const array1 = [1, 2, 3];
// const array2 = [5, 6, 7];
// // const newArray = [...array1, ...array2, 89, 69];
// const newArray = [..."123456789"];
// console.log(newArray);
// spread operator in objects
const obj1 = {
key1: "value1",
key2: "value2",
};
const obj2 = {
key1: "valueUnique",
key3: "value3",
key4: "value4",
};
// const newObject = { ...obj2, ...obj1, key69: "value69" };
// const newObject = { ...["item1", "item2"] };
// const newObject = { ..."abcdefghijklmnopqrstuvwxyz" };
// console.log(newObject);
// object destructuring
const band = {
bandName: "led zepplin",
famousSong: "stairway to heaven",
year: 1968,
anotherFamousSong: "kashmir",
};
let { bandName, famousSong, ...restProps } = band;
console.log(bandName);
console.log(restProps);
// objects inside array
// very useful in real world applications
const users = [
{userId: 1,firstName: 'harshit', gender: 'male'},
{userId: 2,firstName: 'mohit', gender: 'male'},
{userId: 3,firstName: 'nitish', gender: 'male'},
]
for(let user of users){
console.log(user.firstName);
}
// nested destructuring
const users = [
{userId: 1,firstName: 'harshit', gender: 'male'},
{userId: 2,firstName: 'mohit', gender: 'male'},
{userId: 3,firstName: 'nitish', gender: 'male'},
]
const [{firstName: user1firstName, userId}, , {gender: user3gender}] = users;
console.log(user1firstName);
console.log(userId);
console.log(user3gender);
// function
function singHappyBirthday(){
console.log("happy birthday to you ......");
}
function sumThreeNumbers(number1, number2, number3){
return number1 + number2 + number3;
}
// isEven
// input : 1 number
// output : true , false
// function isEven(number){
// return number % 2 === 0;
// }
// console.log(isEven(4));
// function
// input : string
// output: firstCharacter
// function firstChar(anyString){
// return anyString[0];
// }
// console.log(firstChar("zbc"));
// function
// input : array, target (number)
// output: index of target if target present in array
function findTarget(array, target){
for(let i = 0; i<array.length; i++){
if(array[i]===target){
return i;
}
}
return -1;
}
const myArray = [1,3,8,90]
const ans = findTarget(myArray, 4);
console.log(ans);
// function expression
// function singHappyBirthday(){
// console.log("happy birthday to you ......");
// }
const singHappyBirthday = function(){
console.log("happy birthday to you ......");
}
// singHappyBirthday();
const sumThreeNumbers = function(number1, number2, number3){
return number1 + number2 + number3;
}
const ans = sumThreeNumbers(2,3,4);
// console.log(ans);
// function isEven(number){
// return number % 2 === 0;
// }
const isEven = function(number){
return number % 2 === 0;
}
// console.log(isEven(2));
const firstChar = function(anyString){
return anyString[0];
}
const findTarget = function(array, target){
for(let i = 0; i<array.length; i++){
if(array[i]===target){
return i;
}
}
return -1;
}
// arrow functions
// const singHappyBirthday = function(){
// console.log("happy birthday to you ......");
// }
const singHappyBirthday = () => {
console.log("happy birthday to you ......");
}
singHappyBirthday();
const sumThreeNumbers = (number1, number2, number3) => {
return number1 + number2 + number3;
}
const ans = sumThreeNumbers(2,3,4);
console.log(ans);
// const isEven = function(number){
// return number % 2 === 0;
// }
const isEven = number => number % 2 === 0;
console.log(isEven(4));
const firstChar = anyString => anyString[0];
console.log(firstChar("harshit"));
const findTarget = (array, target) => {
for(let i = 0; i<array.length; i++){
if(array[i]===target){
return i;
}
}
return -1;
}
// hoisting
// hello();
// function hello(){
// console.log("hello world");
// }
// console.log(hello);
// const hello = "hello world";
// console.log(hello);
// functions inside function
function app(){
const myFunc = () =>{
console.log("hello from myFunc")
}
const addTwo = (num1, num2) =>{
return num1 + num2;
}
const mul = (num1, num2) => num1* num2;
console.log("inside app");
myFunc();
console.log(addTwo(2,3));
console.log(mul(2,3));
}
app();
// lexical scope
const myVar = "value1";
function myApp(){
function myFunc(){
// const myVar = "value59";
const myFunc2 = () =>{
console.log("inside myFunc", myVar);
}
myFunc2();
}
console.log(myVar);
myFunc();
}
myApp();
// block scope vs function scope
// let and const are block scope
// var is function scope
// if(true){
// var firstName = "Saurabh";
// console.log(firstName);
// }
// console.log(firstName);
function myApp(){
if(true){
var firstName = "Saurabh";
console.log(firstName);
}
if(true){
console.log(firstName);
}
console.log(firstName);
}
myApp();
// default parameters
// function addTwo(a,b){
// if(typeof b ==="undefined"){
// b = 0;
// }
// return a+b;
// }
function addTwo(a,b=0){
return a+b;
}
const ans = addTwo(4, 8);
console.log(ans);
// rest parameters
// function myFunc(a,b,...c){
// console.log(`a is ${a}`);
// console.log(`b is ${b}`);
// console.log(`c is`, c);
// }
// myFunc(3,4,5,6,7,8,9);
function addAll(...numbers){
let total = 0;
for(let number of numbers){
total = total + number;
}
return total;
}
const ans = addAll(4,5,4,2,10);
console.log(ans);
// param destructuring
// object
// react
const person = {
firstName: "Saurabh",
gender: "male",
age: 500
}
// function printDetails(obj){
// console.log(obj.firstName);