-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLoops.html
More file actions
50 lines (41 loc) · 1.02 KB
/
Loops.html
File metadata and controls
50 lines (41 loc) · 1.02 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
<script>
//🟨 Loops : (Let us run some code over and over like a loop)
//🟩Loop Variable
// var i = 1
//🟩Loop Condition
// while (i <= 5) {
// console.log(i)
// i++
// }
// for (var i; i <= 10; i++) {
// console.log(i)
// }
var randomNumber = 0
while (randomNumber < 0.5) {
randomNumber = Math.random()
}
console.log(randomNumber)
const todoList = ["make dinner", "wash dishes", "watch youtube"]
let todoListHTML = ""
for (let i = 0; i < todoList.length; i++) {
const value = todoList[i]
console.log(value)
}
//🟨 Accumulator Pattern :-
//🟩 Create a variable to store the variable
//🟩 Loop through the array and update the result
const nums = [1, 2, 3]
//🟩 Accumulator Variable
let total = 0
for (let i = 0; i < nums.length; i++) {
const num = nums[i]
total += num
}
console.log(total)
const numsDoubled = []
for (let i = 0; i < nums.length; i++) {
const num = nums[i]
numsDoubled.push(num * 2)
}
console.log(`${nums} Converted into ${numsDoubled}`)
</script>