# Basic JavaScript - Use Recursion to Create a Countdown

Tell us what’s happening:
Describe your issue in detail here.

// Only change code below this line
function countdown(n){
if (n < 1) {
return [];
}
else {
let arr = [];
arr.unshift(n);
countdown(n - 1);
return arr;
}
}
// Only change code above this line
console.log(countdown(5))

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

Challenge: Basic JavaScript - Use Recursion to Create a Countdown

in the real solution why does it push the 1st number that is used as the parameter n to the array if the recursion is calling n - 1? like how does it get the first number there if it dosent .unshift() until after its calling it with n - 1? shouldnt it unshift first to push the first number and then decrement n and call it again?

The code you provided does not work.
Recursion works in a reversed way, in a sense that a function calls itself several times and starts returning each return value in a FILO (First-In, Last-Out) order.

Your provided code is doing this:

countdown(5);
let arr = [];
arr.unshift(n);
countdown(n - 1);

countdown(5 - 1 = 4)
countdown(4 - 1 = 3)
countdown(3 - 1 = 2)
countdown(2 - 1 = 1)
countdown(1 - 1 = 0)

return arr;

return []
return [1]
return [2]
return [3]
return [4]
return [5]

You are not returning a single array with the desired numbers, but 6 arrays. You need to apply the unshift() to the same array throughout the recursion.

The example given by freeCodeCamp in the exercise, in fact, is very similar to what you need.

I think I get it. the first time the function is called it runs through the entire function with the original parameter and only activates the n - 1 when it goes to run through it again.
function countdown(n) {
if (n < 1) {
return ;
}
else {
let arr = countdown(n - 1);
arr.unshift(n);
return arr;
}
}
so it will still unshift the first (n) because it has to run through it before going through the recursive call again at which point it will be (n - 1).
Am I understanding this correctly?

Now your algorithm is returning like this:

return []
return [1]
return [2, 1]
return [3, 2, 1]
return [4, 3, 2, 1]
return [5, 4, 3, 2, 1]

This is the order in which the recursion returns. It does not begin to return anything until it calls itself the last time — the stop case (in which n = 1 and countdown(1 - 1 = 0)).

so what do you mean? That it doesn’t actually return anything but the [5,4,3,2,1] array because its the stop case? then what’s your point lol? my question was how it gets the first number into the array if its recalling it with n-1 before it says to unshift n.

Before the unshift, it needs to know what countdown(n - 1) returns, which needs to know what countdown (n - 1) returns, and so on until countdown(0), which returns an empty array. Then countdown(1) unshifts 1 to that emtpy array. Then countdown(2) unshifts 2 to that array, and so on until countdown(n) unshifts n to that array.

so it runs through the function every time storing (n) until it reaches the base case and the can go on to the next step and unshift all the stored numbers?

You’re getting the concept. But the ns aren’t stored up, there’s just several different contexts that each have their own separate n.

When you call countdown(2) from inside countdown(3), the execution needs to sit there and wait to figure out what countdown(2) is before it can assign that to arr and move on to the next line where it unshifts. So if you look at the order of execution line-by-line it’s something like this (plugging in the value of n for clarity):

let arr = countdown(3 - 1) // n=3 context, needs to get countdown(2)
let arr = countdown(2 - 1) // n=2 context, needs to get countdown(1)
let arr = countdown(1 - 1) // n=1 context, needs to get countdown(0)
return [] // n=0 context, returns empty array
let arr = [] // n=1 context, plug in the result of countdown(0)
arr.unshift(1)
return arr
let arr = [1] // n=2 context, plug in the result of countdown(1)
arr.unshift(2)
return arr
let arr = [2, 1] // n=3 context, plug in the result of countdown(2)
arr.unshift(3)
return arr
1 Like

I think I get it. Thanks for taking the time to explain it to me!

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