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

2.0 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
56105e7b514f539506016a5e Count Backwards With a For Loop 1 用For循环向后计数

Description

只要我们可以定义正确的条件for循环也可以向后计数。为了向后计数两次我们需要更改initialization conditionfinal-expression 。我们将从i = 10开始并在i > 0循环。我们将递减i 2每个回路与i -= 2
var ourArray = [];
forvar i = 10; i> 0; i- = 2{
ourArray.push;
}
ourArray现在包含[10,8,6,4,2] 。让我们改变initializationfinal-expression这样我们就可以向后计数两位奇数。

Instructions

使用for循环将奇数从9到1推送到myArray

Tests

tests:
  - text: 你应该为此使用<code>for</code>循环。
    testString: 'assert(code.match(/for\s*\(/g).length > 1, "You should be using a <code>for</code> loop for this.");'
  - text: 你应该使用数组方法<code>push</code> 。
    testString: 'assert(code.match(/myArray.push/), "You should be using the array method <code>push</code>.");'
  - text: '<code>myArray</code>应该等于<code>[9,7,5,3,1]</code> 。'
    testString: 'assert.deepEqual(myArray, [9,7,5,3,1], "<code>myArray</code> should equal <code>[9,7,5,3,1]</code>.");'

Challenge Seed

// Example
var ourArray = [];

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

// Setup
var myArray = [];

// Only change code below this line.

After Test

console.info('after the test');

Solution

// solution required