-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfor_loops.js
76 lines (64 loc) · 1.36 KB
/
for_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
/*
INSTRUCTIONS
Use a for loop to work to push the values 1 through 5 onto myArray.
// Example
var ourArray = [];
for (var i = 0; i < 5; i++) {
ourArray.push(i);
}
*/
// Setup
var myArray = [];
// Only change code below this line.
for (var i = 0; i < 5; i++) {
myArray.push(i+1);
}
console.log(myArray);
/*INSTRUCTIONS
Push the odd numbers from 1 through 9 to myArray using a for loop.
// Example
var ourArray = [];
for (var i = 0; i < 10; i += 2) {
ourArray.push(i);
}
*/
// Setup
var myArray = [];
// Only change code below this line.
for (var i = 0; i < 10; i += 2) {
myArray.push(i+1);
}
console.log(myArray);
/*INSTRUCTIONS
Push the odd numbers from 9 through 1 to myArray using a for loop.
// Example
var ourArray = [];
for (var i = 10; i > 0; i -= 2) {
ourArray.push(i);
}
*/
// Setup
var myArray = [];
// Only change code below this line.
for (var i = 10; i > 0; i -= 2) {
myArray.push(i-1);
}
console.log(myArray);
/* INSTRUCTIONS
Declare and initialize a variable total to 0. Use a for loop to add the value of each
element of the myArr array to total.
// Example
var ourArr = [ 9, 10, 11, 12];
var ourTotal = 0;
for (var i = 0; i < ourArr.length; i++) {
ourTotal += ourArr[i];
}
*/
// Setup
var myArr = [ 2, 3, 4, 5, 6];
// Only change code below this line
var total = 0;
for (var i = 0; i < myArr.length; i++) {
total += myArr[i];
}
console.log (total);