-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforloop.js
More file actions
128 lines (97 loc) · 1.99 KB
/
forloop.js
File metadata and controls
128 lines (97 loc) · 1.99 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
for (let i=0;i<=10;i++) // post increment// initilization // condition//increment
{
console.log(i);
}
///////////////////////////
let x=0;
for (let i=0;i<=10;i++)
{
x+=i; //i=0 x=0 // 0+1=1
console.log(x);// print all values of x
}
//////////////////////////////
let x=0;
for (let i=0;i<=10;i++)
{
x+=i; //i=0 x=0 // 0+1=1
}
console.log(x); // print last value of x
////////////////////////
let arr=["one","two","three","four","five"]
for (let i=0;i<arr.length;i++)
{
console.log(arr[i]);
}
///////////////
let str1="reena";
for (let i=0; i<str1.length;i++)
{
console.log(str1[i]);// to print each value of string
}
/////////////////////////
let str2="reena";
let newstr=""
for (let i=0; i<str2.length;i++)
{
newstr=newstr+str2[i];
// to print horizontal
}
console.log(newstr);
// reverse of string using loop
let str3="reena";
let newstr1=""
for (let i=str3.length-1;i>=0;i--)
{
newstr1=newstr1+str3[i]
}
console.log(newstr1);
// while loop only condition satisfy
// it runs until condition satisfy
let one=1;
while(one<=10)
{
console.log(one);
one++
}
let num=[1,2,3,4,5,9]
let i=0;
while(num[i]!=5)
{
console.log(num[i]);
i++
}
///////////////////////////////
// do while loop
let z=1;
do{
console.log(z);;
z++;
}
while(z<=10)
// if condition fail it executes atleast once
let y=1;
do{
console.log(y);;
y++;
}
while(y>10)
////////////// for in loop use in key word iterates object and its keys
let obj={
name:"reena",age:10
}
for (let a in obj)
{
console.log(a,"--",obj[a]);
}
// for of loop use string and arrays and itsiterates values
let pet=["dog","rabbit","parrot","cat"]
for (let a of pet)
{
console.log(a);
}
//// for each loop arrays iterates only value and index
let numnum=["one","two","three","four"]
numnum.forEach((val,ind,arr)=> // pass val ind and arr
{
console.log(val,ind,arr);
})