DEV Community

loading...

Using Recursion to Create a Countdown

rthefounding profile image Randy Rivera ・1 min read

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.

  • 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.
  • Solution:
function countdown(n) {
  if (n < 1) {
    return [];
  } else {
    var countdownArray = countdown(n - 1);
    console.log(countdownArray)
    countdownArray.unshift(n);
    return countdownArray;
}
}
Enter fullscreen mode Exit fullscreen mode
console.log(countdown(10)); will display [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
Enter fullscreen mode Exit fullscreen mode

Discussion (0)

Forem Open with the Forem app