freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-an.../basic-javascript/count-backwards-with-a-for-...

1.4 KiB
Raw Blame History

id title challengeType videoUrl forumTopicId dashedName
56105e7b514f539506016a5e 使用 For 循环反向遍历数组 1 https://scrimba.com/c/c2R6BHa 16808 count-backwards-with-a-for-loop

--description--

只要我们定义好合适的条件for 循环也可以反向遍历。

为了让每次递减 2我们需要改变 initialization、condition 和 final-expression。

设置 i = 10,并且当 i > 0 的时候才继续循环。 我们使用 i -= 2 来让 i 每次循环递减 2。

const ourArray = [];

for (let i = 10; i > 0; i -= 2) {
  ourArray.push(i);
}

ourArray 现在将包含 [10, 8, 6, 4, 2]。 让我们改变初始值和最后的表达式,这样我们就可以按照奇数从后往前两两倒着数。

--instructions--

使用一个 for循环,把从 9 到 1 的奇数添加到 myArray

--hints--

应该使用 for 循环。

assert(/for\s*\([^)]+?\)/.test(code));

应该使用数组方法 push

assert(code.match(/myArray.push/));

myArray 应该等于 [9, 7, 5, 3, 1]

assert.deepEqual(myArray, [9, 7, 5, 3, 1]);

--seed--

--after-user-code--

if(typeof myArray !== "undefined"){(function(){return myArray;})();}

--seed-contents--

// Setup
const myArray = [];

// Only change code below this line

--solutions--

const myArray = [];
for (let i = 9; i > 0; i -= 2) {
  myArray.push(i);
}