var ourArray = [];
for (var i=10; i > 0; i-=2) {
ourArray.push(i);
}
ourArray will now contain [10,8,6,4,2].
练习
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 = 9; i > 0; i -= 2) {
myArray.push(i);
}
介绍
只要定义了正确的条件,for loop可以倒着数。
方法
ourArray will now contain [10,8,6,4,2].
练习
代码
结果显示
来源
https://www.freecodecamp.org/challenges/count-backwards-with-a-for-loop