-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntroduction.js
More file actions
1595 lines (992 loc) · 30.3 KB
/
Introduction.js
File metadata and controls
1595 lines (992 loc) · 30.3 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("hey this is a console");
console.log("code is running....");
// VARIABLES:-
var a =5;
var b = 4;
console.log(a+b);
var _c="vinit";
console.log(_c);
console.log(typeof a,typeof b ,typeof _c);
// some variable names are invalid in js
// eg: var 55bin="vinit";
// IF-ELSE STATEMENT :-
let age=45;
if(age>18)
{
console.log("you can drive the car");
}
else{
console.log("you can't drive the car");
}
// -----------------------------
a=6;
b=8;
// if(a>b){
// let c=a-b;
// }
// else{
// let c=b-a;
// }
// alternative way for the above statement
let c = a > b ? (a-b) : (b-a)
console.log(c);
//Q4>
let goldenstr = "Annaas";
if ((goldenstr[0] == "a" || goldenstr[0] == "A") && (goldenstr.length > 5)) {
console.log("Golden String");
}
else {
console.log("not a golden string");
}
Q5>
let a = 5;
let b = 1;
let c = 7;
if (a > b) {
if (a > c) {
console.log(a, "is largest");
}
else {
console.log(c, "is largest")
}
}
else {
if (b > c) {
console.log(b, "is largest")
}
else {
console.log(c, "is largest")
}
}
//Q6>
let num1 = 32;
let num2 = 47852;
if (num1 % 10 == num2 % 10) {
console.log(num1 % 10, "same last digit");
}
else {
console.log("diffrent last digit");
}
// -------------------
// LOOPS :-
let l=0;
for(let i=0;i<10;i++){
console.log(l+i)
}
// --
let obj={
name:"vinit",
role:"programmer" ,
comapany:"goldmanschs"
}
for(const key in obj){
console.log(key)
}
// --
let i=0;
while(i<6){
console.log(i)
i++;
}
// --
let p=0;
do{
console.log(p)
p++
}
while(p<6)
// q1> to check if user is eligible to vote or not
let agee = 21;
let citizen = true;
let registered = false;
if (agee >= 18) {
if (citizen == true) {
if (registered == true) {
console.log("You are eligible to vote")
}
else {
console.log("You are not eligible to vote due to registration status")
}
}
else {
console.log("you are not eligible to vote due to cititzenship")
}
}
else {
console.log("You are not eligible to vote due to age limit")
}
// to check if number is even or odd
var num = 7;
if (num % 2 == 0) {
console.log("even")
}
else {
console.log("odd")
}
// switch case problem statement
var areaofshape = "circle";
var r = 5;
var a = 5;
var b = 10;
var result;
switch (areaofshape) {
case "square":
result = a * a;
console.log(result)
break;
case "rectangle":
result = a * b;
console.log(result)
break;
case "circle":
result = 3.14 * r * r;
console.log(result)
break;
default:
console.log("no shape matches")
}
// while loop to count from 1 to 10
var num = 1;
while (num <= 10) {
console.log(num);
num++;
}
// do-while loop to print 1 to 10
var num = 1;
do {
console.log(num);
num++;
}
while (num <= 10)
// for loop to print 1 to 10
for (var num = 1; num <= 10; num++) {
console.log(num);
}
// 5 ka table
var num = 1;
while (num <= 10) {
console.log('5 *' + num + '=' + 5 * num)
num++
}
// to check if number is valid or not
//this will only orint output if number is positive number
let userinput;
let positivenumber;
do {
userinput = prompt("enter any number");
positivenumber = userinput;
}
while (isNaN(positivenumber) || positivenumber < 0);
console.log("you have entered a valid positie number :", positivenumber);
// sum of first 10 nos
var sum = 0;
for (var num = 1; num <= 10; num++) {
var sum = sum + num;
console.log(sum);
}
// to check if prime or not
var num = 13
var isprime = true;
for (var i = 2; i < num; i++) {
if (num % i == 0) {
isprime = false;
break;
}
}
if (isprime) {
console.log("prime");
}
else {
console.log("not prime");
}
// ------------------------
// function :-
function sub(a,b){
return a - b;
}
console.log("subtraction",sub(4,8))
function add(a, b) {
return a + b;
}
console.log(add(10, 24))
// immediately invoked function expression (IIFE)
// : this function executes immediately when a page is loaded ,this function doesn't interfere with surrounding code and executes immediately.
// it is a normal function wrapped inside parenthesis ")" and there is no need to write a function name
(function (a, b) {
console.log(a + b);
})
(5, 7);
// let , var and const
// var keyword is global level scope keyword it can be accessed inside and outside of any scope
if (true) {
var name = " vinit"
console.log(name);
}
console.log(name)
// whereas let is block scope it can only be accessed inside a block
if (true) {
let name = " vinit"
console.log(name);
}
console.log(name)
// const is an immutable keyword it can't be changed , it will throw error if tried to change
const pi = 3.14;
pi = 34;
console.log(pi);
// template literals
// template literals provides convenient way to create strings they are enclosed within
// backtick (``), we can use template literals to write multiline comment and preserve whitespaces.
let multilinestr = `
this is a
multi-line string
using template literals`
console.log(multilinestr);
//string interpolation
//it is used to embed expressions directly into string , for this backtick is required !.
let firstn = "vinit";
let lastn = "gharat";
let fname = `${firstn} ${lastn}`;
console.log(fname);
let age = 20;
let message = `i am ${age} years old`;
console.log(message)
// defaut parameter
// default parameter are used if user does'nt inputs any value incase default specified values is used
function sum(a = 2, b = 4) {
return a + b;
}
console.log(sum(3));
console.log(sum());
console.log(sum(6, 6));
// arrow function
// it is a compact alternative to a traditional function
// traditional
hello1 = function () {
return "hellow";
}
// using arrow function
hello2 = () => "helloworld";
console.log(hello1, hello2);
//implicit return in arrow function
//if arrow function is assigned only single task then we dont write any keyword
//and here we dont use curly braces we use parenthesis !
const mul = (a, b) => a * b;
console.log(mul(2, 3));
// Calculator function
let Calculator = (num1, num2, operator) => {
switch (operator) {
case "+":
return num1 + num2;
case "-":
return num1 - num2;
case "*":
return num1 * num2;
case "/":
return num1 / num2;
default:
return "no operator found";
}
}
console.log(Calculator(5, 4, "+"));
// reverse a string without bulit in method
let isreverse = (str) => {
for (let i = str.length; i >= 0; i--) { //str.length indicates to last element !
console.log(str[i]);
}
};
isreverse("vinit");
// to check if string is palindroe
let ispalin = (str) => {
let reverse = " ";
for (let i = str.length; i >= 0; i--) {
reverse = reverse + str[i];
}
if (str = reverse) {
return true;
}
else {
return false;
}
};
console.log(ispalin("radar"));
// Arrays :-
let arr = [1,2,5,4,3,6,7]
console.log(arr)
console.log(arr.length)
console.log(arr[0])
//arrays are mutable
arr[1] = 0;
console.log(arr.toString()) //to print array as a string
console.log(arr.join("and")) //adds and inplace os comma
// arr.pop() //removes last element from array
// arr.push() //adds element at last
// arr.shift() //removes first element
// arr.unshift() //adds at first
// delete a[6] //removes value fromgiven index
// arr.splice(0,3) //removes array of provided index
arr.sort() //sorts array
arr.reverse() //reverse an array
// Array
let array1 = new Array('apple', 'orange', 'mango')
let array3 = ["apple", "orange", "mango"]
console.log(array1, array3);
console.log(array1[1]);
array1[1] = "kiwi";
console.log(array1);
// iterating using for-of loop
// using for-of we can acess values of array
for (let items of array1) {
console.log(items)
}
// this can be also done using normal for loop
// iterating using for-in loop
// using for-in we can access the indexes of the array
for (let items in array1) {
console.log(items);
}
// for-each
// for-each calls function for every element, and the function
// can perform any kind of operation on that array(affects the original array)!
array1.forEach((curr, index, arr) => {
console.log(`${curr} ${index}`);
});
// map
// map works same as for - each just the diffrence is operations
// performed on array doesn't affect the original array
array1.map((curr, index, arr) => {
console.log(`${curr} ${index}`);
});
const favfruit = array1.map((curr, index, arr) => {
return `my favourite fruit is ${curr}`;
});
console.log(favfruit);
console.log(array1);
// program to multiply every element with 2
const numbers = [1, 2, 3, 4, 5, 6];
numbers.forEach((currElem) => {
console.log(currElem * 2)
})
//Array CRUD operation
let array = ["a", "b", "c", "d"];
//adds at end
console.log(array.push("e"));
console.log(array.push("f"));
console.log(array);
//removes from end
console.log(array.pop());
console.log(array);
//adds at start
console.log(array.unshift("e"));
console.log(array.unshift("f"));
console.log(array);
//removes from start
console.log(array.shift());
console.log(array);
// splice method
// splice method changes the content of an array by removing or replacing existing Elements
// syntax: .splice(index, indexof_item_tobedeleted, item_tobeinserted)
array.splice(2, 0, "A", "B");
console.log(array);
array.splice(1, 2);
console.log(array);
array.splice(1, 1, "gg");
console.log(array);
//index of method
// index of method returns index of element if found else returns -1
// syntax: .indexOf(search element, from index )
let aray1 = ['apple', 'orange', 'mango']
console.log(aray1.indexOf("apple"));
console.log(aray1.indexOf("apple", 2));
// lastIndexOf method
// returns index of array and if there are multiple occurences then gives index of last element
let aray2 = ['apple', 'orange', 'orange', 'orange', 'mango']
console.log(aray2.lastIndexOf("orange"));
// includes method
console.log(aray2.includes("orange"));
//find method : only returns single value
const nos = ["1", "2", "3", "4", "5"];
let result1 = nos.find((currelem) => {
return currelem > 2;
});
console.log(result1);
//filter method: returns all values
let result = nos.filter((currelem) => {
return currelem > 2;
});
console.log(result);
//find index method
let result2 = nos.findIndex((currelem) => {
return currelem > 2;
});
console.log(result2);
// Q> filtering products by price
const products = [
{ name: "laptop", price: 1200 },
{ name: "phone", price: 800 },
{ name: "tablet", price: 300 },
{ name: "smartwatch", price: 150 },
];
const filterproducts = products.filter((currelem) => {
return currelem.price >= 500;
})
console.log(filterproducts);
//sort method
let numb = [1, 2, 4, 3, 4, 7, 5, 6, 9, 8, 1, 7]
let res = numb.sort()
console.log(res);
//compare and sort
const sorted = numb.sort((a, b) => {
if (a > b) {
return 1;
}
else {
return -1;
}
})
console.log(numb);
//destructuring
//it is used to store values of array into multiple variables
let nam3 = ["tony", "bruce", "peter", "vinit", "steve"]
let [ironman, hulk, spidey, ...winterSoldiers] = nam3;
console.log(ironman);
console.log(spidey);
console.log(winterSoldiers);
//spread
//spread expands array into multiple single values and we can use it to iterate through this
//spread is defing using three dots (...)
arrAy = [0, 1, 2, 5, 2, 5, 8, 6, 8, 1, 4, 7, 0, 4, 63,]
console.log(...arrAy);
//using spread we can also use string methods
console.log(Math.min(...arrAy));
console.log(Math.max(...arrAy));
//Rest
//rest's syntax is similar to spread but it functons diffent than spread
// it allows a function to take infinite number of arguments and store them in an array
function test(...args) {
for (let i = 0; i < args.length; i++) {
console.log(args[i]);
}
}
console.log(test(5, 8, 6, 8, 9, 4, 5, 8, 5, 8, 6, 8, 9, 4, 5, 8));
console.log(test(5, 8));
//reduce method
// reduce method iterates over an array and applies fuction to each Element
// syntax: .reduce(function callback(accumulator, index, array) { })
const productsPrice = [100, 3400, 200, 150, 2000];
const totalPrice = productsPrice.reduce((accum, currElem) => {
return accum + currElem;
});
console.log(totalPrice);
// Q> to check if numbers in array are multiples of 10 or not
let nums = [10, 20, 30, 40];
let ans = nums.every((el) =>
el % 10 == 0
);
console.log(ans);
// Q> function to find a minimum number in an array
function getMin(nos) {
let min = nos.reduce((min, el) => {
if (min < el) {
return min;
} else {
return el;
}
});
return min;
}
let nos = [10, 20, 30, 40];
console.log(getMin(nos));
//Strings
//string can be written in " " as well as in ' '
const striing = "hello world"
// to add special characters in the string we use slash (\)
let string = " this is \"vinit\" , i am in \'India\' ,i play \/football\/ ";
console.log(striing, string);
console.log(striing.length);
//convert a string into array
let astring = "apple fruit";
let strArr = Array.from(astring)
console.log(strArr);
// string :-
let nam3="abhishek";
let sn="kumar";
console.log(nam3);
console.log(nam3[5]);
console.log(nam3.length)
console.log("his name is "+nam3+sn);
console.log(`his name is $(nam3) $(sn)`) //template literal
console.log(nam3.toUpperCase)
console.log(nam3.slice(1,4))
console.log(nam3.replace("shek",("shake")))
console.log(nam3.concat(sn,"vinit","gharat"))
console.log(nam3.charAt(4))
console.log(nam3.indexOf("sh"))
console.log(nam3.startsWith("abhi"))
//Search methods
//index of method
// index of method returns index of element if found else returns -1
// syntax: .indexOf(search element, from index )
let str1 = "a is for apple"
console.log(str1.indexOf("apple"));
console.log(str1.indexOf("apple", 10));
// lastIndexOf method
// returns index of array and if there are multiple occurences then gives index of last element
let str2 = "a is for apple"
console.log(str2.lastIndexOf("for"));
// includes method
// returns true or false value if found
console.log(str2.includes("orange"));
//search method
// returns index number of first match found
let str3 = "a is for apple b is for ball"
let resultt = str3.search('for')
console.log(resultt);
//match method
// this method returns an array of the matched values and its info in array format
let str4 = "a is for apple b is for ball";
let result4 = str4.match("for")
console.log(result4);
//matchAll method
//this returns detailed info of the value
let str5 = "a is for apple b is for ball";
let result5 = str4.matchAll("for")
console.log(result4);
//startsWith and endsWith method
// returns true or false if starts or ends with specified value
let str15 = "a is for apple b is for ball";
let result15 = str15.startsWith("a")
let result6 = str15.endsWithWith("for")
console.log(result15, result6);
//string slicing
let str7 = "a is for apple b is for ball";
let result7 = str7.slice(0, 6)
console.log(result7);
//charAt method
// returns character at specified index
let str9 = "a is for apple b is for ball";
let result8 = str9.charAt(5);
console.log(result8);
//replace method
let str10 = "a is for apple b is for ball";
let newstr = str10.replace("apple", "avocado");
console.log(newstr);
//uppercase and lowercase method
let str11 = "a is for Apple b is for BALL";
let result11 = str11.toUpperCase();
let result12 = str11.toLowerCase();
console.log(result11, result12);
//General maths
const piValue = Math.PI;
const roundoffValue = Math.round(3.7);
const ceilValue = Math.ceil(3.7);
const floorValue = Math.floor(3.7);
const truncValue = Math.trunc(3.7);
console.log(piValue, roundoffValue, ceilValue, floorValue, truncValue);
//events
// <div class="inline Event">
// <h1>inline event</h1>
// <button onclick="alert('Welcome , welcome , welcome ! ')">
// Gr33tings4u
// </button>
// <button ondblclick="alert('Welcome to u again')">Dblclick</button>
// <button onmouseover="alert('Welcome again')">Dblclick</button>
// </div>
//using DOM event handler
document.getElementById('btn1').onclick = function () {
alert('swaagat nhi kroge hmara')
};