2018-09-30 23:01:58 +01:00
|
|
|
---
|
|
|
|
id: 56105e7b514f539506016a5e
|
|
|
|
title: Count Backwards With a For Loop
|
|
|
|
challengeType: 1
|
2019-02-14 12:24:02 -05:00
|
|
|
videoUrl: 'https://scrimba.com/c/c2R6BHa'
|
2018-09-30 23:01:58 +01:00
|
|
|
---
|
|
|
|
|
|
|
|
## Description
|
|
|
|
<section id='description'>
|
|
|
|
A for loop can also count backwards, so long as we can define the right conditions.
|
|
|
|
In order to count backwards by twos, we'll need to change our <code>initialization</code>, <code>condition</code>, and <code>final-expression</code>.
|
|
|
|
We'll start at <code>i = 10</code> and loop while <code>i > 0</code>. We'll decrement <code>i</code> by 2 each loop with <code>i -= 2</code>.
|
2019-05-17 06:20:30 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
var ourArray = [];
|
|
|
|
for (var i=10; i > 0; i-=2) {
|
|
|
|
ourArray.push(i);
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2018-09-30 23:01:58 +01:00
|
|
|
<code>ourArray</code> will now contain <code>[10,8,6,4,2]</code>.
|
|
|
|
Let's change our <code>initialization</code> and <code>final-expression</code> so we can count backward by twos by odd numbers.
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Instructions
|
|
|
|
<section id='instructions'>
|
|
|
|
Push the odd numbers from 9 through 1 to <code>myArray</code> using a <code>for</code> loop.
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Tests
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
```yml
|
2018-10-04 14:37:37 +01:00
|
|
|
tests:
|
|
|
|
- text: You should be using a <code>for</code> loop for this.
|
2019-07-13 00:07:53 -07:00
|
|
|
testString: assert(code.match(/for\s*\(/g).length > 1);
|
2018-10-04 14:37:37 +01:00
|
|
|
- text: You should be using the array method <code>push</code>.
|
2019-07-13 00:07:53 -07:00
|
|
|
testString: assert(code.match(/myArray.push/));
|
2018-10-20 21:02:47 +03:00
|
|
|
- text: <code>myArray</code> should equal <code>[9,7,5,3,1]</code>.
|
2019-07-13 00:07:53 -07:00
|
|
|
testString: assert.deepEqual(myArray, [9,7,5,3,1]);
|
2018-09-30 23:01:58 +01:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
```js
|
|
|
|
// Example
|
|
|
|
var ourArray = [];
|
|
|
|
|
|
|
|
for (var i = 10; i > 0; i -= 2) {
|
|
|
|
ourArray.push(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup
|
|
|
|
var myArray = [];
|
|
|
|
|
|
|
|
// Only change code below this line.
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
### After Test
|
|
|
|
<div id='js-teardown'>
|
|
|
|
|
|
|
|
```js
|
2018-10-20 21:02:47 +03:00
|
|
|
if(typeof myArray !== "undefined"){(function(){return myArray;})();}
|
2018-09-30 23:01:58 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
<section id='solution'>
|
|
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
var ourArray = [];
|
|
|
|
for (var i = 10; i > 0; i -= 2) {
|
|
|
|
ourArray.push(i);
|
|
|
|
}
|
|
|
|
var myArray = [];
|
|
|
|
for (var i = 9; i > 0; i -= 2) {
|
|
|
|
myArray.push(i);
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|