fix(curriculum): Improved Use Recursion to Create a Countdown challenge (#37548)

* fix: improved recursive challenge

* fix: rewording of two challenges

* fix: add code tags

Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com>
This commit is contained in:
Randell Dawson
2019-12-02 08:58:07 -08:00
committed by Oliver Eyton-Williams
parent a6766127cc
commit 7454719334
3 changed files with 38 additions and 48 deletions

View File

@ -440,13 +440,13 @@
"587d7b7e367417b2b2512b21", "587d7b7e367417b2b2512b21",
"Use Multiple Conditional (Ternary) Operators" "Use Multiple Conditional (Ternary) Operators"
], ],
[
"5cc0bd7a49b71cb96132e54c",
"Use Recursion to Create a Range of Numbers"
],
[ [
"5cd9a70215d3c4e65518328f", "5cd9a70215d3c4e65518328f",
"Use Recursion to Create a Countdown" "Use Recursion to Create a Countdown"
],
[
"5cc0bd7a49b71cb96132e54c",
"Use Recursion to Create a Range of Numbers"
] ]
], ],
"helpRoom": "HelpJavaScript", "helpRoom": "HelpJavaScript",

View File

@ -8,15 +8,34 @@ forumTopicId: 305925
## Description ## Description
<section id='description'> <section id='description'>
Continuing from the previous challenge, we provide you another opportunity to create a recursive function to solve a problem. In a [previous challenge](/learn/javascript-algorithms-and-data-structures/basic-javascript/replace-loops-using-recursion), you learned how to use recursion to replace a for loop. Now, let's look at a more complex function that returns an array of consecutive integers starting with <code>1</code> through the number passed to the function.
As mentioned in the previous challenge, there will be a <dfn>base case</dfn>. The base case tells the recursive function when it no longer needs to call itself. It is a simple case where the return value is already known. There will also be a <dfn>recursive call</dfn> which executes the original function with different arguments. If the function is written correctly, eventually the base case will be reached.
For example, say you want to write a recursive function that returns an array containing the numbers <code>1</code> through <code>n</code>. This function will need to accept an argument, <code>n</code>, representing the final number. Then it will need to call itself with progressively smaller values of <code>n</code> until it reaches <code>1</code>. You could write the function as follows:
```javascript
function countup(n) {
if (n < 1) {
return [];
} else {
const countArray = countup(n - 1);
countArray.push(n);
return countArray;
}
}
console.log(countup(5)); // [ 1, 2, 3, 4, 5 ]
```
At first, this seems counterintuitive since the value of `n` <em>decreases</em>, but the values in the final array are <em>increasing</em>. This happens because the push happens last, after the recursive call has returned. At the point where `n` is pushed into the array, `count(n - 1)` has already been evaluated and returned `[1, 2, ..., n - 1]`.
</section> </section>
## Instructions ## Instructions
<section id='instructions'> <section id='instructions'>
We have defined a function called <code>countdown</code> with two parameters. The function should take an array in the <code>myArray</code> parameter and append the numbers n through 1 based on the <code>n</code> parameter. We have defined a function called <code>countdown</code> with one parameter (<code>n</code>). The function should use recursion to return an array containing the integers <code>n</code> through <code>1</code> based on the <code>n</code> parameter. If the function is called with a number less than 1, the function should return an empty array.
For example, calling this function with <code>n = 5</code> will pad the array with the numbers <code>[5, 4, 3, 2, 1]</code> inside of it. For example, calling this function with <code>n = 5</code> should return the array <code>[5, 4, 3, 2, 1]</code>.
Your function must use recursion by calling itself and must not use loops of any kind. Your function must use recursion by calling itself and must not use loops of any kind.
</section> </section>
@ -26,13 +45,13 @@ Your function must use recursion by calling itself and must not use loops of any
``` yml ``` yml
tests: tests:
- text: After calling <code>countdown(myArray, -1)</code>, myArray should be empty. - text: <code>countdown(-1)</code> should return an empty array.
testString: assert.isEmpty(padArray([], -1)); testString: assert.isEmpty(countdown(-1));
- text: After calling <code>countdown(myArray, 10)</code>, myArray should contain <code>[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]</code> - text: <code>countdown(10)</code> should return <code>[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]</code>
testString: assert.deepStrictEqual(padArray([], 10), [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]); testString: assert.deepStrictEqual(countdown(10), [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]);
- text: After calling <code>countdown(myArray, 5)</code>, myArray should contain <code>[5, 4, 3, 2, 1]</code> - text: <code>countdown(5)</code> should return <code>[5, 4, 3, 2, 1]</code>
testString: assert.deepStrictEqual(padArray([], 5), [5, 4, 3, 2, 1]); testString: assert.deepStrictEqual(countdown(5), [5, 4, 3, 2, 1]);
- text: Your code should not rely on any kind of loops (<code>for</code> or <code>while</code> or higher order functions such as <code>forEach</code>, <code>map</code>, <code>filter</code>, or <code>reduce</code>.). - text: Your code should not rely on any kind of loops (<code>for</code>, <code>while</code> or higher order functions such as <code>forEach</code>, <code>map</code>, <code>filter</code>, and <code>reduce</code>).
testString: assert(!removeJSComments(code).match(/for|while|forEach|map|filter|reduce/g)); testString: assert(!removeJSComments(code).match(/for|while|forEach|map|filter|reduce/g));
- text: You should use recursion to solve this problem. - text: You should use recursion to solve this problem.
testString: assert(removeJSComments(countdown.toString()).match(/countdown\s*\(.+\)\;/)); testString: assert(removeJSComments(countdown.toString()).match(/countdown\s*\(.+\)\;/));
@ -49,10 +68,10 @@ tests:
//Only change code below this line //Only change code below this line
function countdown(myArray, n){ function countdown(n){
return; return;
} }
console.log(countdown(5)); // [5, 4, 3, 2, 1]
``` ```
</div> </div>
@ -62,11 +81,6 @@ function countdown(myArray, n){
```js ```js
const removeJSComments = str => str.replace(/\/\*[\s\S]*?\*\/|\/\/.*$/gm, ''); const removeJSComments = str => str.replace(/\/\*[\s\S]*?\*\/|\/\/.*$/gm, '');
function padArray(arr, n){
countdown(arr, n);
return arr;
}
``` ```
</div> </div>
@ -78,14 +92,8 @@ function padArray(arr, n){
```js ```js
//Only change code below this line //Only change code below this line
function countdown(myArray, n){ function countdown(n){
if(n <= 0){ return n < 1 ? [] : [n].concat(countdown(n - 1));
return;
}
else{
myArray.push(n);
countdown(myArray, n - 1);
}
} }
``` ```

View File

@ -8,25 +8,7 @@ forumTopicId: 301180
## Description ## Description
<section id='description'> <section id='description'>
In a [previous challenge](/learn/javascript-algorithms-and-data-structures/basic-javascript/replace-loops-using-recursion), you learned how to use recursion to replace a for loop. Now, let's look at a more complex function that returns an array of consecutive integers starting with <code>1</code> through the number passed to the function. Continuing from the previous challenge, we provide you another opportunity to create a recursive function to solve a problem.
As mentioned in the previous challenge, there will be a <dfn>base case</dfn>. The base case tells the recursive function when it no longer needs to call itself. It is a simple case where the return value is already known. There will also be a <dfn>recursive call</dfn> which executes the original function with different arguments. If the function is written correctly, eventually the base case will be reached.
For example, say you want to write a recursive function that returns an array containing the numbers 1 through n. This function will need to accept an argument <code>n</code> representing the final number. Then it will need to call itself with progressively smaller values of <code>n</code> until it reaches 1. You could write the function as follows:
```js
function count(n) {
if (n === 1) {
return [1];
} else {
var numbers = count(n - 1);
numbers.push(n);
return numbers;
}
}
```
At first this is counterintuitive since the value of `n` <em>decreases</em>, but the values in the final array are <em>increasing</em>. This happens because the push happens last, after the recursive call has returned. At the point where `n` is pushed into the array, `count(n - 1)` has already been evaluated and returned `[1, 2, ..., n - 1]`.
</section> </section>