-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaScript_Practice_Set.js
More file actions
1885 lines (1561 loc) · 52.6 KB
/
JavaScript_Practice_Set.js
File metadata and controls
1885 lines (1561 loc) · 52.6 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
//JavaScript Practice Code
// Variables in JavaScript:
let fristname = "shivam"
let lastname = "maurya"
console.log("Hi my name is " +fristname+ ' ' +lastname)
let globalVar = "I am global"; // Global variable
// *******************************************************************************************************************
function example() {
console.log(globalVar); // ✅ Accessible inside the function
}
example();
//console.log(globalVar); // ✅ Accessible outside the function
//********************************************************************************************************************
//sum of two number
let a = 5;
let b = 6;
function addtwonumber() {
var c = a + b;
console.log("Sum of two number is", c)
}
addtwonumber();
/* *************************************************************************************************************** */
//addition of two number
function add(a, b, c) {
console.log("Sum of three number is ", a + b + c)
}
add(3, 4, 5);
// ********************************************************************************************************************
//example of block scope (let)
function username() { //block
let firstname = "shivam"
let lastname = "maurya"
console.log("my name is", firstname, lastname)
} //block
username();
// ********************************************************************************************************************
//example of function scope (var)
function sub(a, b) //Called fuction
{
var a = 5
var b = 2
var c = a - b
console.log("Subtraction of Two number is", c)
}
sub(); // Calling function
// ********************************************************************************************************************
//Example of block scope (cost)
function mul() {
const h = 7
const e = 5
const g = h * e
console.log("Multiplication of two digit is", g)
}
mul()
// ********************************************************************************************************************
// Example of object
let person = {
name: "Alice", // "name" is the key, "Alice" is the value
age: 25, // "age" is the key, 25 is the value
isStudent: true // "isStudent" is the key, true is the value
};
console.log(person.name, person.age, person.isStudent)
// ********************************************************************************************************************
//Example of Array
let ramayana = ["ram", "sita", "lakshman"];
console.log(ramayana);
console.log(typeof ramayana);
console.log(ramayana.length);
console.log(Array.isArray(ramayana));
let fruit = ["mango", "orange", "apple"]
console.log(fruit)
console.log(typeof fruit)
// ********************************************************************************************************************
/*
// var is hoisted,that means it is moved to the top during execution but remains undefined until assigned.
console.log(a); // ✅ undefined (Variable is hoisted)
var a = 5;
console.log(a); // ✅ 5
*/
// ********************************************************************************************************************
/*
// example of let (let cannot be redeclared)
let name6 = "shivam";
console.log(name6);
let name6 = "raju"; // ❌ SyntaxError: Identifier 'city' has already been declared
console.log(name6); // ❌ SyntaxError: Identifier 'city' has already been declared
*/
// ********************************************************************************************************************
/*
// Write a function that returns another function until all three numbers are provided?
function mul1(p) {
return function mul2(q) {
return function mul3(r) {
return p * q * r
}
}
}
console.log(mul1(2)(4)(8))
// alert("this may cause an issue later...");
*/
// ********************************************************************************************************************
/*
// Example of template literal in javascript
let lastfournumber = 7876;
var templateliteral1 = `last four digit of my account number ` + lastfournumber
console.log(templateliteral1)
let templateliteral = `last four digit of my account number `.concat(lastfournumber)
console.log(templateliteral)
let lastfournumberOfAcc = "8855";
let strtemplateliteral = `My account number is ${lastfournumberOfAcc.padStart(16, '*')}.`
console.log(strtemplateliteral);
let text = "My name is Shivam";
console.log(text.indexOf("Shivam"));
*/
// ********************************************************************************************************************
/*
// Example of charCodeAt() string method in JavaScript
let text1 = "Shivam";
console.log(text1.charCodeAt(1)); // 104
let text = "Shivam";
let index = 0; // Specify index
console.log(`Unicode code (ASCII) of '${text.charAt(index)}' at index ${text.indexOf(text.charAt(index))} is: ${text.charCodeAt(index)}`);
*/
// ********************************************************************************************************************
/*
// Example of Split() method:
let fruits = "apple,banana,mango,orange";
console.log(fruits.split(","));
let text = "apple,banana,cherry";
let fruits = text.split(",");
console.log(fruits);
*/
// ********************************************************************************************************************
/*
// Example of Palindrome in JavaScript :
let name4 = prompt("Enter your name :");
let rev = name4.split("").reverse().join("");
if (name4 == rev) {
console.log("Palindrome");
}
else {
console.log("Not palindrome");
}
*/
// ********************************************************************************************************************
/*
// Incremental and Decremental operators in JavaScript :
let x = 5;
x++;
console.log(x);
let y = 5;
y--;
console.log(y);
//Another Example of Incremental and Decremental operators:
let e = 5;
e++;
console.log(e);
e--;
console.log(e);
let y = 21;
y %= 2; // y = y % 2 → y = 1
console.log(y);
*/
// ********************************************************************************************************************
/*
//logical AND (&&) Example in javascript :
let mathMark = 88;
if (mathMark >= 80 && mathMark <= 90) { // both conditions are true in this example.
console.log("You Scored between 80 - 90 in Mathematics.");
} else {
console.log("You Scored less than 80 or more than 90 in Mathematics.");
}
*/
// ********************************************************************************************************************
/*
//logical OR (||) Example in javascript :
let englishMark = 92;
if (englishMark >= 90 || englishMark >= 95) { // At least one condition is true in this example.
console.log("You Scored between 90 - 95 Mark in English");
} else {
console.log("You Scored outside the range 90 - 95 in English.");
}
// logical OR (||) Example in javascript :
let ChemistryMark = 96;
if (ChemistryMark <= 90 || ChemistryMark <= 95) { // both conditions are false in this example.
console.log("You Scored between 90 - 95 in Chemistry.");
} else {
console.log("You Scored outside the range (90 - 95) in Chemistry.");
}
*/
// ********************************************************************************************************************
/*
// IF Statement Example in javascript :
const username = prompt('Please Enter Your Name.');
const userAge = parseInt(prompt('Please Enter Your Age.'));
console.log(`Name: ${username}`);
console.log(`Age: ${userAge}`);
if (userAge >= 0 && userAge <= 4) {
console.log(`${username} is a kid.`);
console.log('And he/she is playing.');
}
if (userAge >= 5 && userAge <= 17) {
console.log(`${username} is a school student.`);
console.log('And he/she is learning science and maths.');
}
if (userAge >= 18 && userAge <= 24) {
console.log(`${username} is a college student.`);
console.log('And he/she is learning computer science.');
}
if (userAge >= 25 && userAge <= 45) {
console.log(`${username} is a working professional.`);
console.log('And he/she is a web developer.');
}
if (userAge > 45) {
console.log(`${username} is retired.`);
console.log('And he/she reads newspaper.');
}
*/
// ********************************************************************************************************************
/*
//Else If Statement Example in javascript
const username1 = prompt('Please Enter Your Name.');
const userAge1 = parseInt(prompt('Please Enter Your Age.'));
console.log(`Name: ${username1}`);
console.log(`Age: ${userAge1}`);
if (userAge1 >= 0 && userAge1 <= 4) {
console.log(`${username1} is a kid.`);
console.log('And he/she is playing.');
}
else if (userAge1 >= 5 && userAge1 <= 17) {
console.log(`${username1} is a school student.`);
console.log('And he/she is learning science and maths.');
}
else if (userAge1 >= 18 && userAge1 <= 24) {
console.log(`${username1} is a college student.`);
console.log('And he/she is learning computer science.');
}
else if (userAge1 >= 25 && userAge1 <= 45) {
console.log(`${username1} is a working professional.`);
console.log('And he/she is a web developer.');
}
else if (userAge1 > 45) {
console.log(`${username1} is retired.`);
console.log('And he/she reads newspaper.');
}
*/
// ********************************************************************************************************************
/*
//Example of Ternary Operator Inside a Function:
function checkPass(marks) {
return marks >= 65 ? "Pass" : "Fail";
}
console.log(checkPass(75));
console.log(checkPass(55));
//Multiple Conditions in a Ternary Operator:
let result = 98;
let grade = result >= 90 ? `Your Grade is A. Because you have Scored: ${result}` :
result >= 80 ? `Your Grade is B. Because you have Scored: ${result}` :
result >= 70 ? `Your Grade is C. Because you have Scored: ${result}` :
"You are Fail. Because you have Scored less than 65.";
console.log(grade);
// Simple example of ternary operator :
let Gender = 'Female'
let UserGender = Gender === 'Female' ? "She is a Frontend Web Developer." : "He is a Frontend Web Developer."
console.log(UserGender)
//Example of ternary operator using template literal:
let gender = 'F'
let userGender = `${gender === 'F' ? "Pooja" : "Pawan"} is a Frontend Web Developer.`
console.log(userGender)
//Example of Ternary Operator using Truthy and Falsy Value in JavaScript:
let gender2 = 'F'
let userGender2 = `${null ? "Pooja" : "Pawan"} is a Frontend Web Developer.`
console.log(userGender2)
*/
// ********************************************************************************************************************
/*
//converting uppercase to lowercase using .toLoweCase();
// Here, why we have used .toLoweCase(), because if we enter capital (F) and we have given small (f) in our condition,
// js compiler will treat is as different value and our condition will become false, so to make capital "F" in smaller "f"
// we need to use .toLoweCase() string method in our code.
let gender1 = 'M'
let userGender1 = `${gender1.toLocaleLowerCase() === 'f' ? "Mamta" : "Viahal"} is a Frontend Web Developer.`
console.log(userGender1)
*/
// ********************************************************************************************************************
/*
//Example of an Object in JavaScript:
const user1 = {
firstName: "Shivam",
lastName: "Maurya",
Age: 22,
isGraduate: true,
}
console.log(user1);
console.log(user1.firstName);
console.log(user1.isGraduate);
console.log(user1["Age"]);
console.log(user1["lastName"]);
user1.city = "Bangalore";
user1.Age = 23;
console.log(delete user1.lastName);
console.log(user1);
*/
// ********************************************************************************************************************
/*
// Creating Object using New Object() in JavaScript:
let userInfo = new Object();
userInfo.name = "Shivam";
userInfo.age = 22;
userInfo.city = "Bangalore";
console.log(userInfo);
*/
// ********************************************************************************************************************
/*
// Creating Object using Constructor Function in JavaScript:
function user(name, age, city, isStudent) {
this.name = name;
this.age = age;
this.city = city;
this.isStudent = isStudent;
}
let user1 = new user("Shivam", 22, "Bangalore", false);
let user2 = new user("Satyam", 18, "Ayodhya", true);
console.log(user);
console.log(user2);
*/
// ********************************************************************************************************************
/*
// Creating Object using **Object.Create()** (Prototype-Based Inheritance in JavaScript:
// Define a Prototype Object
const UserPrototype =
{
greet: function () {
console.log(`Hello Guys!!, My name is ${this.name}, And I'm ${this.age} years old.`);
}
};
// Create an Object Using Object.create()
let User1 = Object.create(UserPrototype);
// Add Properties to the Object
User1.name = "Shivam";
User1.age = 22;
User1.isStudent = false;
// Call the Inherited Method
User1.greet();
// Output: Hello, my name is John
*/
// ********************************************************************************************************************
/*
// Another example of Creating an Object Using Object.Create() method in JavaScript:
// Step 1: Define a Prototype Object
const personPrototype = {
greet: function () {
console.log(`Hello, my name is ${this.name}`);
}
};
// Step 2: Create an Object Using Object.create()
let person1 = Object.create(personPrototype);
// Step 3: Add Properties to the Object
person1.name = "John";
person1.age = 25;
person1.isStudent = false;
// Step 4: Call the Inherited Method
person1.greet(); // Output: Hello, my name is John
// Step 5: Create Another Object Using the Same Prototype
let person2 = Object.create(personPrototype);
person2.name = "Alice";
person2.age = 22;
person2.isStudent = true;
// Step 6: Call the Inherited Method for the Second Object
person2.greet(); // Output: Hello, my name is Alice
console.log(person2.name);
console.log(person1);
console.log(person2.age);
*/
// ********************************************************************************************************************
/*
// using Bracket Notation to access property in object
// Define an object
let user2 = {
"full name": "Shivam",
"last-name": "Maurya",
age: 22,
country: "India"
};
// // Access properties using bracket notation
console.log(user2["full name"]);
console.log(user2["last-name"]);
console.log(user2["age"]);
console.log(user2["country"]);
*/
// ********************************************************************************************************************
/*
// Example of Modifying an Object
let vegetables = {
name: "potato",
color: "Brown",
weight: "1kg",
isOrganic: true,
}
console.log(vegetables);
// modifying existing properties :
vegetables.name = "tomato";
vegetables["color"] = "red"
console.log(vegetables);
*/
// ********************************************************************************************************************
/*
// Adding new properties to the object :
let car = {
brand: "Toyota",
model: "camry",
}
console.log(car);
car.year = 2022;
car["color"] = "Red";
console.log(car);
*/
// ********************************************************************************************************************
/*
//Prevent Adding New Property by using Object.preventExtensions() method
let user = {
name: "Shivam",
age: 22,
isStudent: false,
}
console.log(user);
Object.preventExtensions(user);
user.birthYear = 2002; // Doesn't Allows Adding New Property in this Object:
user.age = 23; // Modifying Existing Properties is Allowed
delete user.isStudent; // Deleting existing Properties is Allowed
console.log(user);
*/
// ********************************************************************************************************************
/*
// Prevents Adding or Deleting properties By using Object.Seal() method in Object in JavaScript:
let laptop = {
brand: "DELL",
Price: 55000,
color: "Black",
}
console.log(laptop);
// Prevents Adding or Deleting properties
Object.seal(laptop);
laptop.weight = "1.4kg"; // Not Allowed
delete laptop.color; // Not Allowed
laptop.Price = 58000; // Allowed
console.log(laptop);
*/
// ********************************************************************************************************************
/*
// Prevents all modifications by using Object.freeze() method in object in javascript:
let score = {
math: 36,
english: 40,
hindi: 45,
isPass: true,
}
console.log(score);
// preventing all modifications in an object in javascript :
Object.freeze(score); // Completely Lock an Object
score.science = 45; // Adding New Property is Not Allowed
delete score.isPass; // Modifying Existing Properties is Not Allowed
score.hindi = 48; // Deleting Properties is Not Allowed
console.log(score);
*/
// ********************************************************************************************************************
/*
//Object Methods: Object.keys(), Object.values(), Object.entries():
let laptop = {
brand: "MSI",
Price: 42000,
color: "silver",
weight: "1.4kg",
}
console.log(Object.keys(laptop));
console.log(Object.values(laptop));
console.log(Object.entries(laptop));
console.log(laptop);
*/
// ********************************************************************************************************************
/*
// Object Cloning (Copying Objects):
let newLaptop = { ...laptop };
console.log(newLaptop);
*/
// ********************************************************************************************************************
/*
//Object Destructuring (Extracting Properties)
let person = {
name: "John",
age: 25,
isStudent: false
};
let { name, age } = person;
console.log(name);
console.log(age);
*/
// ********************************************************************************************************************
/*
// Nested Object in javascript :
let student = {
name: "Shivam",
age: 22,
Scores:
{
Math: 35,
English: 40,
Science:
{
Biology: 48,
Chemistry: 40,
Physics: 38,
}
}
}
console.log(student.Scores.Science.Biology);// 48
console.log(student.name);// Shivam
console.log(student.Scores.Science);// {Biology: 48, Chemistry: 40, Physics: 38}
console.log(student.Scores);// {Math: 35, English: 40, Science: {…}}
console.log(student.Scores.English);
*/
// ********************************************************************************************************************
/*
//Stack memory in object in javascript :
let a = 10;
let b = a; // A copy of 'a' is created in stack memory
b = 20;
console.log(a); // Output: 10
console.log(b); // Output: 20
*/
// ********************************************************************************************************************
/*
// Example of Heap memory in object in javascript :
let user = {
name: "Shivam",
age: 22,
city: "Bangalore",
}
console.log(user);
let user1 = user;
user1.name = "Satyam";
user1.age = 18;
user1.city = "Ayodhya";
console.log(user);
console.log(user1);
*/
// ********************************************************************************************************************
/*
// Example of Shallow copy in object in javascript:
let obj1 = {
name: 'Shivam',
age: 22,
city: 'Bangalore',
}
console.log(obj1);
let obj2 = obj1;
obj2.age = 23;
console.log(obj1);
console.log(obj2);
*/
// ********************************************************************************************************************
/*
// Example of Deep copy in object in javascript:
let user1 = {
name: "shivam",
age: 22,
address: {
city: "Bangalore",
state: "Karnataka",
}
};
let user2 = JSON.parse(JSON.stringify(user1));
user2.address.city = "Mangalore";
user2.age = 23;
console.log(user1);
console.log(user2);
*/
// ********************************************************************************************************************
/*
// Example of Garbage Collection in Object in JavaScript:
let user = {
name: "shivam",
age: 22
}
user = null;
console.log(user);
*/
// ********************************************************************************************************************
/*
// An Example of An Array in JavaScript:
let weekDays = ["Mon", "Tue", "Wed", "Thus", "Fri", "Sat", "Sun"];
console.log(weekDays);
*/
// ********************************************************************************************************************
/*
// Creating an Array Using Square Brackets([]) in JavaScript:
let fruits = ["mango", "apple", "Banana"];
console.log(fruits);
*/
// ********************************************************************************************************************
/*
// Creating an Array Using new Array() method in JavaScript:
let colors = new Array("Red", "Green", "Blue");
console.log(colors);
*/
// ********************************************************************************************************************
/*
// If we pass a single number inside new Array(), it creates an empty array of that length, instead of an array with one number.
let numbers = new Array(5);
console.log(numbers);
*/
// ********************************************************************************************************************
/*
//Creating an Empty Array:
//We can also create an empty array and add elements later.
let evenNumbers = [];
evenNumbers.push(2);
evenNumbers.push(4);
evenNumbers.push(6);
evenNumbers.push(8);
evenNumbers.push(10);
console.log(evenNumbers);
*/
// ********************************************************************************************************************
/*
//Storing Different Data Types in An Array in JavaScript?
let mixedArray = ["Hello", 25, true, { name: "John" }, [1, 2, 3]];
console.log(mixedArray);
*/
// ********************************************************************************************************************
/*
// Multidimensional Array (2D Array Or Nested Array) in JavaScript :
const TicTocToe = [['X', null, 'O'], [null, 'O', 'X'], ['X', null, 'X']]
console.log(TicTocToe);
console.log(TicTocToe[0][0]);
*/
// ********************************************************************************************************************
/*
// Higher Dimensional Array (3D Array Or Array inside array inside array) in JavaScript :
let cube = [
[
[1, 2],
[3, 4]
],
[
[5, 6],
[7, 8]
]
];
console.log(cube[1][0][1]);
*/
// ********************************************************************************************************************
/*
// while loop example in js :
let Friends = ["shivam", "subhash", "ravi", "anupam", "shravan"];
let i = 0;
while (i < Friends.length)
{
console.log(`${i}. ${Friends[i]} maurya`);
i++
}
console.log("Program Ended:")
// while loop example in js :
let Friend = ["shivam", "subhash", "ravi", "anupam", "shravan"];
let j = 0;
while (j < Friend.length) {
Friend[j] = Friend[j] + " maurya"
console.log(`${j + 1}. ${Friend[j]}`)
j++;
}
let i = 1; // Initialization
while (i <= 5) { // Condition
console.log("Iteration:", i);
console.log(i++); // Update (prevents infinite loop)
}
// printing 10 to 1 using while loop ;
let i = 10;
while (i >= 1) {
console.log(i);
i--; // Update (prevents infinite loop)
}
*/
// ********************************************************************************************************************
/*
// printing 10 to 1 using do...while loop in JavaScript:
let i = 10;
do {
console.log(i);
i--; // Update (prevents infinite loop)
}
while (i >= 1);
*/
// ********************************************************************************************************************
/*
// sum of 1 to 5 number using for loop in JavaScript:
let sum = 0;
for (let num = 1; num <= 5; num++) {
sum = sum + num;
}
console.log("Sum of first 5 numbers:", sum);
*/
// ********************************************************************************************************************
/*
// print even numbers till given Number using for loop in javascript :
for (let num = 2; num <= 20; num++) {
if (num % 2 == 0) {
console.log(num);
}
}
*/
// ********************************************************************************************************************
/*
// Print even numbers from the Given Array using for loop in javaScript:
let num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
for (let i = 0; i < num.length; i++) {
if (num[i] % 2 == 0) {
console.log(num[i])
}
}
*/
// ********************************************************************************************************************
/*
//Example of for...of loop with array in javascript :
let colors = ["Red", "Green", "Blue"];
for (let color of colors) {
console.log(color);
}
console.log("for...of loop with array in javascript:")
*/
// ********************************************************************************************************************
/*
//Example of for...of loop with "String" in javascript:
let Myname = "Shivam";
for (let letter of Myname) {
console.log(letter);
}
console.log("for...of loop with String in javascript:")
*/
// ********************************************************************************************************************
/*
//Example of for...in loop with "Object" in javascript:
let user = {
name: "Shivam",
age: 22,
city: "Bangalore"
};
for (let key in user) {
console.log(`${key}: ${user[key]}`);
}
// Example of for...in loop with Array (Though for...in is not recommended for arrays, it still works by iterating over the index numbers) in javascript
let colors = ["Red", "Green", "Blue"];
for (let i in colors) {
console.log(i, colors[i]);
}
*/
// ********************************************************************************************************************
/*
// Break Statement in for loops in javascript :
for (let i = 1; i <= 10; i++) {
if (i === 5) {
break;
}
console.log(i)
}
*/
// ********************************************************************************************************************
/*
// continue Statement in for loops in javascript :
for (let i = 1; i <= 10; i++) {
if (i === 5) {
continue;
}
console.log(i)
}
*/
// ********************************************************************************************************************
/*
// Using break and continue in while Loop in Javascript:
let i = 0;
while (i < 10) {
i++;
if (i === 6) {
continue; // Skips 6
}
if (i === 10) {
break; // Stops at 9
}
console.log(i);
}
*/
// ****************************************** Function ******************************************************
/*
//Function Declaration In JavaScript:
function Intro() // Function Keyword and Function Name
{
console.log("Hi!");// Code to run / Function Statement
console.log("My Name Is Shivam Maurya.");// Code to run / Function Statement
console.log("I am a Frontend Web Developer.");// Code to run / Function Statement
}
Intro(); // Function Call
*/
// ********************************************************************************************************************
/*
//Function Argument and Parameter:
function myIntro(name, Profession)//(name) & (Profession) are parameters inside "myIntro()" function:
{
console.log(`Hii!!, My Name Is ${name}.`);
console.log(`I am a ${Profession}.`);
}
myIntro("Shivam Maurya", "Frontend Web Developer"); // Function call with Argument value (Shivam Maurya) & (Frontend Web Developer) to that Parameter
myIntro("Satyam Maurya", "Student");//Function call with Argument value (Satyam Maurya) & (Student) to that Parameter
myIntro("Shubham Maurya", "Doctor");//Function call with Argument value (Shubham Maurya) & (Doctor) to that Parameter
*/
// ********************************************************************************************************************
/*