var ourArray = [];
for (var i = 0; i < 10; i += 2) {
ourArray.push(i);
}
ourArray will now contain [0,2,4,6,8].
练习
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 = 1; i < 10; i +=2) {
myArray.push(i);
}
介绍
For loops不仅能一次遍历一个数,改变
fianl-expression
,我们可以计算偶数。方法
ourArray will now contain [0,2,4,6,8].
练习
代码
结果显示
来源
https://www.freecodecamp.org/challenges/iterate-odd-numbers-with-a-for-loop