Use Recursion to Create a Countdown - using var?

Tell us what’s happening:
How come this isn’t working? The suggested sollutions use const, but I can’t work out why this isn’t right?

  **Your code so far**

// Only change code below this line
function countdown(n){

var myArray = [];

  
if (n <= 0) {
  return [];
} else if (n == 1) {
  myArray.push(n);
  return myArray
} else {
  myArray.push(n);
  n = n - 1;
  countdown(n)
}

return myArray;

}



// Only change code above this line
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36.

Challenge: Use Recursion to Create a Countdown

Link to the challenge:

You are redefining this array every time you call your function. That won’t work. You need to return the array and use the return value of each function call.

1 Like

Hi there! First of all, each time you call the function the variable myArray is constructed over and over again. Instead of is, you should create it inside of an if statement and assign the function itself to it and then do the correct method on the array. You may need something opposite to .push() in this case :wink:

1 Like

There is also a different way to do this that would allow you to use something similar to what you have at the minute.

The problem you have here is that you’re creating a new empty array every time the function runs. If you added a second parameter to the function that was the array, then you could put a value into that, and pass that along every time you called countdown. So it would be like countdown(n, arr), and you’d start it off like countdown(5, []). Except the challenge requires you to only have one parameter when countdown first gets called, so you can’t just that.

If you’ve gone through the JS course in order, then you’ve already been shown a way to get around this in the ES6 section.

1 Like

The ES6 section comes after this one!

I’ve spent all night on this one and the next challenge (https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/use-recursion-to-create-a-range-of-numbers), and I’m still really struggling to understand even the solutions I can find.

For example with this solution - I don’t understand where the array that we push things onto is created :thinking: and it looks to be like the variable is both a function, and somehow also an array that we’re pushing things to - but that doesn’t make sense to me?

Really sorry if these are silly questions! I must have watched a dozen different explanations of it on Youtube already ha

function rangeOfNumbers(startNum, endNum) {
if (startNum == endNum){
  return [startNum];
} else {
   var arr = rangeOfNumbers(startNum + 1, endNum);
   arr.unshift(startNum);
   return arr;
}
};

The array is created here…

and returned back out here to be used…

Overall flow is like this

When the recursion reaches the end case, it returns [3].
range2

This result is passed back to the caller, which is rangeOfNumbers(2,3). The else part of this one will do
range3

Finally, the else part of rangeOfNumbers(1, 3) will do
range4

One array is created at the end case and each recursive step adds (unshift) a number to it.

One last thing. There’s no significance of arr being var here. It could be declared const. What is important here is that you use a local reference to an array. You can make use of a global variable and skip local references but using global for recursive functions is not generally a recommended approach.

Another important thing here is that the local reference is set to a result coming back from a recursive call. You don’t create a local array (like you’re doing in the opening post) because if you do that, then you’ll lose what’s passed back from the recursive call.

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