Basic JavaScript - Use Recursion to Create a Countdown

Tell us what’s happening:

I am stumped on this one. The errors I am getting are:
countdown(10) should return [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
countdown(5) should return [5, 4, 3, 2, 1]
Global variables should not be used to cache the array.

Your code so far

// Only change code below this line
function countdown(n) {
  if (n < 1) {
    return [];
  } else {
    const countArray = countdown(n + 1);
    countArray.push(n);
    return countArray;
  }
}
// Only change code above this line

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.4 Safari/605.1.15

Challenge Information:

Basic JavaScript - Use Recursion to Create a Countdown

The biggest issue right here is that you are essentially calling this function forever.

You’re not going to hit at a return statement for the condition in the else block to occur.

Here’s what needs to happen. You need a way to first store the number you already have. Then call the function and add all the elements in the array you got back into your new one without using any kind of loops.

There is a method in the Array class that will let you do that. Here’s the documentation.

Good luck and happy learning. :slight_smile:

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.