-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjavascript.js
1115 lines (725 loc) · 20.4 KB
/
javascript.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
<!-- JavaScript Fundamentals -->
<!-- JavaScript -->
JavaScript is a powerful programming language that can add interactivity to a website. It was invented by Brendan Eich.
JavaScript is versatile and beginner-friendly. With more experience, you'll be able to create games, animated 2D and 3D graphics, comprehensive database-driven apps, and much more!
JavaScript Can Change HTML Content.
The alert function in JavaScript displays a message to the user which they can then dismiss. To show where this would fit into an actual HTML document, here’s an example of a simple page with some JavaScript:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Hello</title>
<script>
alert('Hello, world!');
</script>
</head>
<body>
<h1>Hello!</h1>
</body>
</html>
<!-- or -->
One of many JavaScript HTML methods is getElementById().
The example below "finds" an HTML element (with id="demo"), and changes the element content (innerHTML) to "Hello JavaScript":
Example-
document.getElementById("demo").innerHTML = "Hello JavaScript";
<!DOCTYPE html>
<html>
<body>
<h2>By using JavaScript</h2>
<p id="demo">JavaScript can change HTML content.</p>
<button type="button" onclick='document.getElementById("demo").innerHTML = "Hello JavaScript!"'>Click Me!</button>
</body>
</html>
output-
JavaScript can change HTML content.
Click Me! with a button.
<!-- introduction -->
<!-- The “script” tag
JavaScript programs can be inserted almost anywhere into an HTML document using the <script> tag.
For instance: -->
<!DOCTYPE HTML>
<html>
<body>
<p>Before the script...</p>
<script>
alert( 'Hello, world!' );
</script>
<p>...After the script.</p>
</body>
</html>
<!-- To attach several scripts, use multiple tags: -->
<script src="/js/script1.js"></script>
<script src="/js/script2.js"></script>
<!-- variables may hold values that have different data types: -->
Variable Explanation Example
String This is a sequence of text known as a string. To signify that the value is a string, enclose it in single or double quote marks. let myVariable = 'Bob'; or
let myVariable = "Bob";
Number This is a number. Numbers don't have quotes around them. let myVariable = 10;
Boolean This is a True/False value. The words true and false are special keywords that don't need quote marks. let myVariable = true;
Array This is a structure that allows you to store multiple values in a single reference. let myVariable = [1,'Bob','Steve',10];
Refer to each member of the array like this:
myVariable[0], myVariable[1], etc.
Object This can be anything. Everything in JavaScript is an object and can be stored in a variable. Keep this in mind as you learn. let myVariable = document.querySelector('h1');
All of the above examples.
<!-- Operators -->
An operator is a mathematical symbol that produces a result based on two values (or variables). In the following table, you can see some of the simplest operators, along with some examples to try in the JavaScript console.
Operator Explanation Symbol(s) Example
Addition Add two numbers together or combine two strings. + 6 + 9;
'Hello ' + 'world!';
Subtraction, Multiplication, Division These do what you'd expect them to do in basic math. -, *, / 9 - 3;
8 * 2; // multiply in JS is an asterisk
9 / 3;
Assignment As you've seen already: this assigns a value to a variable. = let myVariable = 'Bob';
Strict equality This performs a test to see if two values are equal. It returns a true/false (Boolean) result. === let myVariable = 3;
myVariable === 4;
Not, Does-not-equal This returns the logically opposite value of what it precedes. It turns a true into a false, etc.. When it is used alongside the Equality operator, the negation operator tests whether two values are not equal. !, !==
For "Not", the basic expression is true, but the comparison returns false because we negate it:
let myVariable = 3;
!(myVariable === 3);
"Does-not-equal" gives basically the same result with different syntax. Here we are testing "is myVariable NOT equal to 3". This returns false because myVariable IS equal to 3:
let myVariable = 3;
myVariable !== 3;
<!-- Conditionals -->
Conditionals are code structures used to test if an expression returns true or not. A very common form of conditionals is the if...else statement. For example:
let iceCream = "chocolate";
if (iceCream === "chocolate") {
alert("Yay, I love chocolate ice cream!");
} else {
alert("Awwww, but chocolate is my favorite…");
<!-- Functions -->
Functions are a way of packaging functionality that you wish to reuse. It's possible to define a body of code as a function that executes when you call the function name in your code. This is a good alternative to repeatedly writing the same code. You have already seen some uses of functions. For example:
let myVariable = document.querySelector("h1");
Copy to Clipboard
alert("hello!");
<!-- Events -->
Real interactivity on a website requires event handlers. These are code structures that listen for activity in the browser, and run code in response. The most obvious example is handling the click event, which is fired by the browser when you click on something with your mouse. To demonstrate this, enter the following into your console, then click on the current webpage:
document.querySelector("html").addEventListener("click", function () {
alert("Ouch! Stop poking me!");
<!-- *variable declaration in javascript -->
var firstName = 'rahim';
console.log (firstName);
<!-- *never use keywords in variable declaration in javascript -->
var ifelse = 10;
console.log(ifelse);
<!-- *data types in javascript -->
String.
Number.
Bigint.
Boolean.
Undefined.
Null.
Symbol.
Object.
<!-- String. -->
var firstName = 'rahim';
console.log (typeof firstName);
<!-- Number. -->
var rahim = 10;
console.log (typeof rahim);
<!-- Bigint. -->
<!-- Boolean. -->
var rahimAge10 = true;
console.log (typeof rahimAge10);
<!-- Undefined. -->
var firstName;
console.log (typeof firstName);
<!-- Null. -->
<!-- Symbol. -->
<!-- Object. -->
var rahim = {
name:'rahim',
age: 10,
};
console.log (typeof rahim);
<!-- * string to number convertion by parseInt use for full digit like '20' string -->
var num = '20';
const convertnum = parseInt(num);
console.log(num);
var num1 = 10;
var num2 = '20';
const convertNum2 = parseInt(num2);
const sum = num1 + convertNum2
console.log(sum);
<!-- * string to number convertion by parseFloat use for full digit like '20.333332' string -->
var num = '20.333332';
const convertnum = parseFloat(num);
console.log(num);
var num1 = 10;
var num2 = '20.333332';
const convertNum2 = parseFloat(num2);
const sum = num1 + convertNum2
console.log(sum);
<!-- * string to number convertion no need for multiplication use any digit -->
var num1 = 10;
var num2 = '20.333332';
const sum = num1 * num2
console.log(sum);
<!-- * control or limit fraction digits -->
var num1 = 10;
var num2 = 20.333332;
const sum = num1 + num2
console.log(sum.toFixed(2));
<!-- * control or limit fraction digits -->
var num1 = 10;
var num2 = 20.333332;
const sum = num1 * num2
console.log(sum.toFixed(2));
<!-- * control or limit fraction digits -->
var num1 = 10;
var num2 = 20.333332;
const sum = num1 + num2;
console.log(sum.toFixed(1));
<!-- * if --------------- -->
var name = 'hablu';
if(name == 'maklu'){
console.log('this is maklu');
}
if(name == 'hablu'){
console.log('this is hablu');
}
<!-- * if/else if --------------- -->
var name = 'hablu';
if(name == 'maklu'){
console.log('this is maklu');
}
else if(name == 'hablu'){
console.log('this is hablu');
}
<!-- * if/else if/else --------------- -->
var name = 'hablu';
if(name == 'maklu'){
console.log('this is maklu');
}
else if(name == 'taklu'){
console.log('this is hablu');
}
else{
console.log('no......');
}
<!-- * if/else if/else if --------------- -->
var num = 20;
if(num == '20'){
console.log('this is 20');
}
else if(num == '100'){
console.log('this is 100');
}
<!-- * if/else if/else if --------------- -->
var name = 'hablu';
if(name == 'maklu'){
console.log('this is maklu');
}
else if(name == 'taklu'){
console.log('this is hablu');
}
else if(name == 'hablu'){
console.log('this is hablu');
}
<!-- * if/else if/else if with &&-------------- -->
var num1 = 20;
var num2 = 30;
if(num1 == 20 && num2 == 40){
console.log('this is 50');
}
else if(num1 == 20 && num2 == 30){
console.log('it is 50');
}
<!-- * if/else if/else with &&-------------- -->
var num1 = 20;
var num2 = 30;
if(num1 == 20 && num2 == 40){
console.log('this is 50');
}
else if(num1 == 20 && num2 == 60){
console.log('it is 50');
}
else{
console.log('nothing is correct');
}
<!-- * if/else if with ||-------------- -->
var num1 = 20;
var num2 = 30;
if(num1 == 20 || num2 == 40){
console.log('this is 50');
}
else if(num1 == 10 || num2 == 40){
console.log('it is 50');
}
<!-- * if/else if/else with ||-------------- -->
var num1 = 20;
var num2 = 30;
if(num1 == 20 || num2 == 40){
console.log('this is 50');
}
else if(num1 == 20 || num2 == 60){
console.log('it is 50');
}
else{
console.log('nothing is correct');
}
<!-- * if/else if/else with > < ------------- -->
var num1 = 100;
if(num1 > 100){
console.log('this is 100');
}
else if(num1 < 100){
console.log('it is 100');
}
else{
console.log('none of above');
}
<!-- * if/else if/else with > < ------------- -->
var num1 = 100;
if(num1 > 100){
console.log('this is 100');
}
else if(num1 < 100){
console.log('it is 100');
}
else{
console.log('yes it equal 100');
}
<!-- * if/else if/else with > = < ------------- -->
var num1 = 100;
if(num1 > 100){
console.log('this is 100');
}
else if(num1 < 100){
console.log('it is 100');
}
else if(num1 >= 100){
console.log('may be 100');
}
else{
console.log('none of above');
}
<!-- * if/else if/else with > && < -------------- -->
var num1 = 100;
var num2 = 200;
var num3 = 300;
if(num1 > num2 && num1 > num3){
console.log('num1 is biggest');
}
else if(num2 > num3){
console.log('num2 is biggest');
}
else{
console.log('num3 is biggest');
}
<!-- * if/else if/else with > && < -------------- -->
var num1 = 300;
var num2 = 500;
var num3 = 400;
if(num1 > num2 && num1 > num3){
console.log('num1 is biggest');
}
else if(num2 > num3){
console.log('num2 is biggest');
}
else{
console.log('num3 is biggest');
}
<!-- * if/else if/else with > && < -------------- -->
var num1 = 600;
var num2 = 400;
var num3 = 500;
if(num1 > num2 && num1 > num3){
console.log('num1 is biggest');
}
else if(num2 > num3){
console.log('num2 is biggest');
}
else{
console.log('num3 is biggest');
}
<!-- *nested elements of if/if else ---- -->
var number = '30';
if(number == 30){
if (typeof number == 'string'){
console.log('all ok');
}
}
<!-- *nested elements of if/else ---- -->
var number = 30;
if(number == 30){
if (typeof number == number){
console.log('all ok');
}
else {
console.log('number is 30 but not string');
}}
else {
console.log('wrong');
}
console.log(typeof number == 'string');
<!-- *nested elements of if/else ---- -->
var number = '30';
if(number == 30){
if (typeof number == 'string'){
console.log('all ok');
}
else {
console.log('number is 30 but not string');
}}
else {
console.log('wrong');
}
console.log(typeof number == 'string');
<!-- *number even or odd------- -->
var num = 100;
if (num % 2 == 1){
console.log('this is odd number');
}
else{
console.log('this is even number');
}
<!-- *number even or odd------- -->
var num = 100;
if (num % 2 != 0){
console.log('this is odd number');
}
else{
console.log('this is even number');
}
<!-- *grading system------------- -->
var result = 85;
if(result >= 80){
console.log('A+');
}
else if(result >=70){
console.log('A');
}
else if(result >= 60){
console.log('-A');
}
else if(result >=50){
console.log('B');
}
else if(result >=40){
console.log('C');
}
else {
console.log('F');
}
<!-- *grading system my logic----------- -->
0-32=F
33-49=D
50-60=C
60-69=B
70-79=A
80-100=A+
var number = 75;
if(number >= 80){
console.log('A+');
}
else if(number >= 70){
console.log('A');
}
else if(number >= 60){
console.log('B');
}
else if(number >= 50){
console.log('C');
}
else if(number >= 40){
console.log('D');
}
else if(number < 40){
console.log('F');
}
<!-- *grading system------------- -->
0-32=F
33-49=D
50-60=C
60-69=B
70-79=A
80-100=A+
var number = 85;
if(number <= 100 && number >= 80){
console.log('A+');
}
else if(number < 80 && number >= 70){
console.log('A');
}
else if(number < 70 && number >= 60){
console.log('B');
}
else if(number < 60 && number >= 50){
console.log('B');
}
else if(number < 50 && number >= 40){
console.log('D');
}
else if(number < 40 && number >= 0){
console.log('F');
}
<!-- *array numbers length check ----- -->
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
console.log(numbers.length);
<!-- *array numbers length check ----- -->
var numbers = [1, 'abul', 3, 'tabul', 5, 6, 'gol', 8, 'mal', 0];
console.log(numbers.length);
<!-- *array numbers index check ----- -->
var numbers = [1, 'abul', 3, 'tabul', 5, 6, 'gol', 8, 'mal', 0];
console.log(numbers[3]);
<!-- *array numbers position check indexof ----- -->
var numbers = [1, 'abul', 3, 'tabul', 5, 6, 'gol', 8, 'mal', 0];
console.log(numbers.indexOf('tabul'));
<!-- *array push---add number or string in last position ----------------- -->
var numbers = [1, 'abul', 3, 'tabul', 5, 6, 'gol', 8, 'mal', 0];
numbers.push('tal');
console.log(numbers);
<!-- *array pop---less number or string from last position ----------------- -->
var numbers = [1, 'abul', 3, 'tabul', 5, 6, 'gol', 8, 'mal', 0, 'tal'];
numbers.pop('tal');
console.log(numbers);
// or--
var numbers = [1, 'abul', 3, 'tabul', 5, 6, 'gol', 8, 'mal', 0, 'tal'];
numbers.pop();
console.log(numbers);
<!-- *array unshift---add number or string at first position ----------------- -->
var numbers = [1, 'abul', 3, 'tabul', 5, 6, 'gol', 8, 'mal', 0, 'tal'];
numbers.unshift(0);
console.log(numbers);
<!-- *array shift---remove number or string from first position ----------------- -->
var numbers = [0, 1, 'abul', 3, 'tabul', 5, 6, 'gol', 8, 'mal', 0, 'tal'];
numbers.shift(0);
console.log(numbers);
// or--
var numbers = [0, 1, 'abul', 3, 'tabul', 5, 6, 'gol', 8, 'mal', 0, 'tal'];
numbers.shift();
console.log(numbers);
<!-- *splice check -----splice 3,5 means index 0,1,2,3
position hold 4, to last 5 degit--- -->
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
console.log(numbers.splice(3,5));
<!-- *slice check -----slice 3,5 means index 0,1,2
position hold 3, to up to mentioned degit 6,
cutting like cake--- -->
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
console.log(numbers.slice(2,6));
<!-- *concat ['a', 'b'] ['c', 'd'];
combination of array -->
var number1 = ['a', 'b', 'u', 'l'];
var number2 = ['t', 'a', 'b', 'l'];
number = number1.concat(number2);
console.log(number);
<!-- *reverses an array's elements---
reverse all array number or string -->
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
console.log(numbers.reverse());
<!-- *toString combain array elements----- -->
var num = 123;
var newNum = String(num);
console.log(newNum);
var bool = true;
newBool = bool.toString();
console.log(newBool);
var obj = {name: "John"};
newObj = obj.toString();
console.log(newObj);
<!-- *join("-"); array elements----- -->
let message = ['abul', 'is', 'good'];
let joinedMessage = message.join(" ");
console.log(joinedMessage);
<!-- *loop-to perform repeated tasks based on a condition is called loop------- -->
JavaScript supports different kinds of loops:
for -
loops through a block of code a number of times.
[syntax-The for loop starts with a for statement followed by
a set of parameters inside the parenthesis]
for/in -
loops through the properties of an object.
for/of -
loops through the values of an iterable object.
while -
loops through a block of code while a specified condition is true.
<!-- *loop syntax----------- -->
for (var i = 0; i < 10; i++){
console.log(i);
}
output-
1
2
3
4
5
6
7
8
9
<!-- *array solveing by loop syntax----------- -->
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9,];
for (var i = 0; i < numbers.length; i++){
var element = numbers [i];
console.log(element);
}
output-
1
2
3
4
5
6
7
8
9
// or ---
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9,];
for (var i = 0; i < numbers.length; i++){
var element = numbers [i];
if (element == 5){
console.log('salary is 5k')
}
}
output-
salary is 5k
// or ------
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9,];
for (var i = 0; i < numbers.length; i++){
var element = numbers [i];
if (element == 5){
console.log('salary is 5k')
}
console.log(element);
}
output-
1
2
3
4
salary is 5k
5
6
7
8
9
// or ------
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9,];
var temp = [];
for (var i = 0; i < numbers.length; i++){
var element = numbers [i];
if (element % 2 == 0){
temp.push(element);
}
}
console.log(temp);
oputput-
[ 2, 4, 6, 8 ]
// or ----
const friends = ['mahi', 'robin', 'ratul', 'mim'];
for (var i = 0; i < friends.length; i++){
const element = friends[i];
console.log(element);
}
output-
mahi
robin
ratul
mim
// or ----
const friends = ['mahi', 'robin', 'ratul', 'mim'];
for (var i = 0; i < friends.length; i++){
const element = friends[i];
if (element == 'mahi'){
console.log('she is good');
}
}
output-
she is good
<!-- Loops- -->
<!-- are used in JavaScript to perform repeated tasks based on a condition. -->
JavaScript supports different kinds of loops:
for -
loops through a block of code a number of times.
for/in -
loops through the properties of an object.
for/of -
loops through the values of an iterable object.
while -
loops through a block of code while a specified condition is true.
<!-- *for loop ------ -->
for (var i = 0; i < 10; i++){
console.log('welcome');
}
output-
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
welcome
<!-- *loop array -------- -->
var friends = ['min', 'robin', 'jabbar', 'borkot'];
for (var i = 0; i < friends.length; i++){
var element = friends[i];
console.log(i);
}
output-
0
1
2
3
<!-- * Array Symbol= [] = square bracket
Object Symbol = {} = curly bracket
to contain the list = () = parentheses -->
<!-- *Object --------- -->
var human = {
name: 'rahim',
age: 25,
friends: ['hero', 'alom', 'robin', 'manna'],
country: 'bangladesh',
}
console.log(human.name);
output-
rahim
// or ---
var human = {
name: 'rahim',
age: 25,
friends: ['hero', 'alom', 'robin', 'manna'],
country: 'bangladesh',
}
console.log(human.friends);
output-
[ 'hero', 'alom', 'robin', 'manna' ]
// or ---
var human = {
name: 'rahim',
age: 25,