zilongxuan001 / LearnFreecode

0 stars 0 forks source link

Count Backwards With a For Loop #262

Open zilongxuan001 opened 6 years ago

zilongxuan001 commented 6 years ago

介绍

只要定义了正确的条件,for loop可以倒着数。

方法

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);
}

结果显示

image

来源

https://www.freecodecamp.org/challenges/count-backwards-with-a-for-loop