-
Notifications
You must be signed in to change notification settings - Fork 2
/
loops.js
118 lines (94 loc) · 2.09 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
/*for(var i=0;i<5;i++){
console.log(i); //0 1 2 3 4
}*/
/*for(var i=4;i<=8;i++){
console.log(i); //4 5 6 7 8
}*/
// decending printing
/*for(var i=10;i>=1;i--){
console.log(i); // 10 9 8 7 6 5 4 3 2 1
}*/
//printing table using for loop through user input :
/*var num=parseInt(prompt("enter a number for table ?"));
for(var i=1;i<=10;i++){
console.log(num+" X "+i+" = "+(num*i));
}*/
// breake and continue
/*for(var i=0;i<7;i++){
if(i==3)
continue; // 0 1 2 4 5 6
console.log(i);
}*/
/*
// prime numbers
var num=parseInt(prompt("enter a number to check "));
var isprime=true;
for(var i=2;i<num;i++){
result=num%i;
if(result==0){
console.log("this number : " + num +" is not prime ! "+ " checked at "+i);
isprime=false;
break;
}
}
if(isprime){
console.log("this number : " + num +" is prime ! "+ " checked at "+i);
}*/
/*for(var i=0;i<5;i++){
for(var j=0;j<3;j++){
console.log("I : "+i+" J : "+j);
}
}*/
/*
var i=0;
do{
console.log(i);
i++;
}while(i < 10);
*/
/*
// while loop example another
var cond=true;
while(cond){
console.log("hey there !");
var asking=prompt("want to continue press Y or y to exit press other key...");
if(asking.toLowerCase() !== "y"){
cond=false;
}
}
console.log("loop breaks game exit!");//:63 hey there ! loop breaks game exit!
*/
// for of loop
let arr=[1,2,4,6,7,8,9,2,3,44,44,6,3,1,6,7,9,56,33];
for(let array of arr ){
console.log(array);
}
//for of example 02 :
const cars = ["BMW", "Volvo", "Mini"];
let text = "";
for (let x of cars) {
text += x + "\t";
}
document.getElementById("demo").innerHTML=text;
// for in loop
let garih = {
year : 2020,
model : "A8",
color : "black",
engine : "V8"
};
for(let atr in garih){
console.log(atr);
}
let arrried=["ahmed","ali","ansari"];
arrried.forEach(element => {
console.log(element);
});
let arrOfArrays = [];
for (let i = 0; i < 3; i++){
arrOfArrays.push([]);
for (let j = 0; j < 7; j++) {
arrOfArrays[i].push(j);
}
}
console.table(arrOfArrays);