freeCodeCamp/curriculum/challenges/english/02-javascript-algorithms-an.../basic-javascript/use-recursion-to-create-a-c...

3.9 KiB

id title challengeType forumTopicId
5cd9a70215d3c4e65518328f Use Recursion to Create a Countdown 1 305925

Description

In a previous challenge, 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 1 through the number passed to the function.

As mentioned in the previous challenge, there will be a base case. 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 recursive call 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, n, representing the final number. Then it will need to call itself with progressively smaller values of n until it reaches 1. You could write the function as follows:

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 decreases, but the values in the final array are increasing. 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].

Instructions

We have defined a function called countdown with one parameter (n). The function should use recursion to return an array containing the integers n through 1 based on the n 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 n = 5 should return the array [5, 4, 3, 2, 1]. Your function must use recursion by calling itself and must not use loops of any kind.

Tests

tests:
  - text: <code>countdown(-1)</code> should return an empty array.
    testString: assert.isEmpty(countdown(-1));
  - text: <code>countdown(10)</code> should return <code>[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]</code>
    testString: assert.deepStrictEqual(countdown(10), [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]);
  - text: <code>countdown(5)</code> should return <code>[5, 4, 3, 2, 1]</code>
    testString: assert.deepStrictEqual(countdown(5), [5, 4, 3, 2, 1]);
  - 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));
  - text: You should use recursion to solve this problem.
    testString: assert(removeJSComments(countdown.toString()).match(/countdown\s*\(.+\)/));

Challenge Seed



//Only change code below this line
function countdown(n){
  return;
}
console.log(countdown(5)); // [5, 4, 3, 2, 1]

After Test

const removeJSComments = str => str.replace(/\/\*[\s\S]*?\*\/|\/\/.*$/gm, '');

Solution

//Only change code below this line
function countdown(n){
   return n < 1 ? [] : [n].concat(countdown(n - 1));
}