-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathloops.js
138 lines (111 loc) · 2.39 KB
/
loops.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
/* Loops are used to repeatedly execute a block of code a certain number of times
Loops :
For loop
While loop
Do while loop
Control flow statements:
Break - to break/exit a loop when particular condition is satisfied
Continue - to skip the current iteration of loop when particular condition is satisfied
const arr= [1,2,3,4,5];
for(let i=0;i<=arr.length-1;i++){
if(i<2){
break;
}
console.log(arr[i]);
}
do while loop
do{
...code
}while(condition)
const arr=[1,2,3,4,5]
let i=-1;
do{
console.log(arr[i])
i++;
}while(i>0 && i<=arr.length-1)
While loop
while(condition){
...code
}
let i=0;
while(i<=arr.length-1){
console.log(arr[i]);
i++;
}
For loop :
for (initialization; condition; increment/decrement) {
code to be executed
}
const arr=[1,2,3,4,5]
for(let i=0;i<=arr.length-1;i++){
console.log(arr[i])
}
for..of loop
for(let element of arr){
console.log(element)
}
const obj={
name:'cams',
age:'23'
}
for..in
for(let index in arr){
console.log(arr[index])
}
const obj={
name:'cams',
age:'23'
}
const objKeys=Object.keys(obj);
console.log(objKeys) // ['name','age']
for(let i=0;i<objKeys.length;i++){
console.log(objKeys[i]) // 'name' , 'age'
const oneKey=objKeys[i]
console.log(obj[oneKey]) // 'cams', 23
}
const arr=[[1,2],[3,4]];
console.log(arr[0][0]) //1
const obj={
address:{
area: 'sr nagar'
}
}
console.log(obj['address']['area']) // sr nagar
/*Nested Loops:
Nested loops are loops within loops.
They are useful when you need to perform operations on multi-dimensional data like array of arrays,
or when you need to combine multiple iterative processes
for(let i=2;i<=3;i++){
for(let j=1;j<=10;j++){
console.log(`${i} * ${j} =`,i*j)
}
}
const arr1=[[1,2,3,4,5], ['a','b','c','d','e']];
for(let i=0;i<arr1.length;i++){
const innerArr=arr1[i];
console.log(innerArr)
for(let j=0;j<innerArr.length;j++){
console.log(innerArr[j])
}
console.log(`----------${i} index completed ------------`)
}
for(let arr1Elem of arr1){
for(let innerArrElem of arr1Elem){
console.log(innerArrElem)
}
}
*/
// const arr=[1,2,3,4,5]
// let i=-1;
// do{
// console.log(arr[i])
// i++;
// }while(i>=0 && i<=arr.length-1)
const arr= [1,2,3,4,5];
for(let i=0;i<=arr.length-1;i++){
console.log(arr)
if(i>2){
continue;
}
console.log(arr[i]);
}