Repository navigation
Expand file tree
/
Copy path08_loops_methods.js
More file actions
125 lines (121 loc) · 3.21 KB
/
Copy path08_loops_methods.js
File metadata and controls
125 lines (121 loc) · 3.21 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
// loops example for loop
let count = 10;
for(let i = 0; i<=count ; i++){ // write the counting for 10
console.log("count: ", i);
}
//2. while and
let idx = 0;
while(idx<20){
if(idx % 2 == 0){
console.log("even number: ", idx);
}else{
console.log(" odd number: ", idx);
}
idx++;
}
// 3. do while loop
console.log("do while loop example");
let index = 1;
do{
console.log(index);
index++;
}while( index<= 5);
//4.Nested if else statement
for (let a = 1; a<3 ; a++){
for(let b= 1; b<3; b++){
console.log(`${a} x ${b} = ${a*b}`);
}
}
//5. for loop with break
for (let i = 0; i<10; i++){
if(i==5) break; // terminate at i =5
console.log(i);
}
//6. for loop with continue
for (let k =1; k<=5; k++){
if(k ==3) continue; // 3 will be skip
console.log(k);
}
//7. for of loop [ fecting the length of the array]
let fruits = [ "mango","apple", "grapes"];
let name = "Aman";
for(let fruit of fruits){
console.log (fruit);
}
for(let char of name){
console.log(char);
}
//8. for in loops [ is for the objects ]
let user = {name:"Aman", age: 19, city:"uttar pradesh "};
for ( let key in user){
console.log(` ${key} : ${user[key]}`);
}
for (let idx in fruits){
console.log(`index ${idx} : ${fruits[idx]} `);
}
//9. for each method
let city = [ " Dehli ", "pune" , "mumbai"];
city.forEach(function(value, index){
console.log(`index ${index} : ${value}`);
}
)
// foreach with arrow function
city.forEach((value, index) => {
console.log(`index ${index} : ${value}`);
}
)
//10. for of loops with entries
console.log("for of loop with entries ")
for (let [value,index] of fruits.entries()){
console.log([value,index]);
}
//. for of loop with map
let map = [ ["name", "Aman"], ["age", 19], ["city", "uttar pradesh"]];
for (let [key, value] of map){
console.log(`${key} : ${value}`);
}
// 11. for of loop with set
let set = new Set(["mango", "apple", "grapes"]);
for(let key of set){
console.log(key);
}
//12. comparing for of anfd for in
console.log(" for of loop pick the value ")
for(let fruit of fruits) console.log(fruit);
console.log(" for in loop pickss the index ");
for(let fruit in fruits) console.log(fruits[fruit]);
//13. reverse for loop (countdown)
console.log("reverse loop / countdown example");
for(let r = 10; r>=1; r--){
console.log("counting down: ", r);
}
//14. labeled loop with break (break outer loop from inner loop)
console.log("labeled loop with break example");
outerLoop:
for(let m = 1; m<=3; m++){
for(let n = 1; n<=3; n++){
if(n == 2) break outerLoop; // stops BOTH loops, not just inner
console.log(`m=${m}, n=${n}`);
}
}
//15. labeled loop with continue (skip outer iteration from inner loop)
console.log("labeled loop with continue example");
outerLoop2:
for(let p = 1; p<=3; p++){
for(let q = 1; q<=3; q++){
if(q == 2) continue outerLoop2; // jumps to next p, skips rest of inner loop
console.log(`p=${p}, q=${q}`);
}
}
//16. while loop for Fibonacci series
console.log("fibonacci series using while loop");
let fibCount = 8;
let a1 = 0, b1 = 1;
let fibIndex = 0;
while(fibIndex < fibCount){
console.log(a1);
let next = a1 + b1;
a1 = b1;
b1 = next;
fibIndex++;
}