-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11-arrays-and-loops.html
158 lines (110 loc) · 3.04 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<!DOCTYPE html>
<html lang="en">
<head>
<title>Arrays and Loops</title>
</head>
<body>
<script>
/*
const myArray = [10, 20, 30];
console.log(myArray[1]);
myArray[0] = 99;
console.log(myArray);
[1, 'helo', true, {name: 'socks'}, [1,2]]
console.log(typeof [1,2]);
console.log(Array.isArray([1,2]));
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);
};
*/
/*
// called accumulator pattern:
const nums = [1,1,3];
let total = 0;
for(let i = 0; i<nums.length; i++){
total = total + nums[i];
}
console.log(total);
// all items in the array should be doubled:
const numsDoubled = [];
for(let i = 0; i<nums.length; i++){
numsDoubled.push(2 * nums[i]);
}
console.log(numsDoubled);
*/
const array1 = [1,2,3];
// creates one array storing values and array2 points to the same value array1 points to
const array2 = array1;
// const array2 = array1.slice();
// ^ creates copy of value in array 1 and creates new separate reference for array2s
array2.push(4);
console.log(array1);
console.log(array2);
const[firstValue, secondValue] = [1,2,3];
// break and continue
// break = exit a loop early
// continue= skip one iteration
for(let i = 1; i <= 10; i++){
// skip loop if divisible by 3
if (i % 3 === 0 ){
continue;
}
}
let i = 0;
while(i<=10){
// ensure to add i++ if u use continue in while loop
if (i %3 ==0){
i++;
continue;
}
console.log(i);
i++;
}
function doubleArray(nums){
let total = 0;
for(let i = 0; i<nums.length; i++){
total = total + nums[i];
}
console.log(total);
// all items in the array should be doubled:
const numsDoubled = [];
for(let i = 0; i<nums.length; i++){
if (nums[i] == 0){
return numsDoubled;
}
numsDoubled.push(2 * nums[i]);
}
return numsDoubled;
}
console.log(doubleArray([1,1,3]));
console.log(doubleArray([1,1,0]));
</script>
</body>
</html>