-
Notifications
You must be signed in to change notification settings - Fork 734
/
Copy path11-arrays-and-loops.html
132 lines (107 loc) · 2.44 KB
/
11-arrays-and-loops.html
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
<!DOCTYPE html>
<html>
<head>
<title>Arrays and Loops</title>
</head>
<body>
<script>
/*
const myArray = [
10,
20,
30
];
console.log(myArray);
console.log(myArray[1]);
myArray[0] = 99;
console.log(myArray);
[1, 'hello', true, { name: 'socks' }, [1, 2]]
console.log(typeof [1, 2]);
console.log(Array.isArray([1, 2]));
console.log(myArray.length);
myArray.push(100);
console.log(myArray);
myArray.splice(0, 1);
console.log(myArray);
*/
/*
let i = 1;
while (i <= 5) {
console.log(i);
i++;
}
for (let i = 1; i <= 5; i++) {
console.log(i);
}
let randomNumber = 0;
while (randomNumber < 0.5) {
randomNumber = Math.random();
}
console.log(randomNumber);
*/
/*
const todoList = [
'make dinner',
'wash dishes',
'watch youtube'
];
for (let i = 0; i < todoList.length; i++) {
const value = todoList[index];
console.log(value);
}
*/
/*
const nums = [1, 1, 3];
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(numsDoubled);
*/
const array1 = [1, 2, 3];
const array2 = array1.slice();
array2.push(4);
console.log(array1);
console.log(array2);
const [firstValue, secondValue] = [1, 2, 3];
for (let i = 1; i <= 10; i++) {
if (i % 3 === 0) {
continue;
}
console.log(i);
if (i === 8) {
break;
}
}
let i = 1;
while (i <= 10) {
if (i % 3 === 0) {
i++;
continue;
}
console.log(i);
i++;
}
function doubleArray(nums) {
const numsDoubled = [];
for (let i = 0; i < nums.length; i++) {
const num = nums[i];
if (num === 0) {
return numsDoubled;
}
numsDoubled.push(num * 2);
}
return numsDoubled;
}
console.log(doubleArray([1, 1, 3]));
console.log(doubleArray([2, 2, 5, 0, 5]));
</script>
</body>
</html>