# Use Recursion to Create a Countdown JS HELP!

Hi, i have big trouble understandig this exercise.

I have this solution (which pass the test perfectly):

``````function countdown(n){
if (n < 1) {
return [];
} else {
const countArray = countdown(n - 1);
countArray.unshift(n);
return countArray;
}
}
console.log(countdown(3));
``````

And my first question is, how does the code understand that “countArray” is an array if i didnt defined it?

Then, I want to know how it works… So, I will supose i use n=3

``````function countdown(n){
if (n < 1) {
return [];
} else {
const countArray = countdown(n - 1);    **// for n=3 ==> countArray = countdown(2)**
countArray.unshift(n);                  **//applying the unshift ==> what happen??????, i know that it add "3" to de first space of countArray, but count array was previusly "countdown(2)... so i really dont understand what happen**
return countArray; **// now here... if i didnt understand the previus step of course i dont know what will return....**
}
}
console.log(countdown(3));  // returns [3, 2, 1]
``````

When execution reaches the first line, it doesn’t go immediately to the second line, but gets paused and executes `countdown` again (and again and again) with `(n-1)` until base case (`n < 1`) is reached. Base case returns an empty array which is assigned to `countArray`, `n` is added to `countArray` and it is returned (and returned and returned).

Thanks for your help, but i think i dont get it. I understand that you say this:

When execution reaches the first line, it doesn’t go immediately to the second line, but gets paused and executes `countdown` again (and again and again) with `(n-1)` until base case ( `n < 1` ) is reached.

By that I understand (using n=3):

const countArray = countdown(2); // excecution #1 with n=3
const countArray = countdown(1); // excecution #2 with n=2
const countArray = countdown(0); // excecution #3 with n=1
because n<1 // excecution #4 returns [ ]

Then you say:

Base case returns an empty array which is assigned to `countArray` , `n` is added to `countArray` and it is returned (and returned and returned).

I understand that my last “n” was n=0, ==> countArray = [ ] … and the real answer is [3 , 2 , 1]

I really dont get how works the code I’ll try to explain it the way I understand it. So let’s say` n = 3` like you said.

The conditional `if (n < 1) { return [] }` sees that n > 1 and runs the rest of the function. The next line in the else condition is what’s important here.

`const countArray = countdown(n-1)`

The program will immediately call countdown(n-1) here and won’t do anything else until that function returns a value. But until n = 1, it just keeps returning itself. Here’s exactly what happens when `n=2`.

`const countArray = countdown(n-1) //at this point, n -1 = 1. So inside this function call, the following happens.`

`if (n <1) { return []} //this passes, returning an empty array and assigning it the variable "countArray"`

This is where countArray is defined as an array.

You are right on the edge of understanding it.

Think about what happens when `countdown(1)` calls `countdown(0)` and gets back the empty array in `countArray`. The next line then executes, unshifting `1` onto the front of the array. It then returns the array `` to `countdown(2)`, which is assigned to the `countArray` variable in `countdown(2)` (which is different than the one in `countdown(1)`. Next line executes, and now `2` is unshifted onto the front of the array and `[2, 1]` is returned to `countdown(3)`, and the process repeats again.

This process continues until all of the function calls are returned.

Sorry … but my brain will exploit.

I think i am understanding a bit more, but if i say that I understand 70% i will be lying.

``````if (n <1) { return []} //this passes, returning an empty array and assigning it the variable "countArray"
``````

Here you said "returning an empty array and assigning it the variable “countArray”… Then if I call console.log(countArray) i will get an empty array countArray = [ ] ? But the answer for n=2 must be [2 , 1] Where are you calling console.log(countArray)? Inside your function or after it’s finished running?

i am not calling the console, i am thinking what will happen if I call it es you say in your statament “returning an empty array and assigning it the variable “countArray”” so if for all the “n” the empty array is assigned to the variable countArray my final result will be an empty array. Why it would be other way? Is really confused for me

In an expression, everything to the right of the equal sign is evaluated first.

`let x = 5 + 9 //The program adds 5 and 4 first, gets 9, and then assigns that value to x.`

The same applies to functions.

`let y = foo() //this program will run foo first, only assigning its return value to y once foo is completed`

So when you call countdown(2), here’s what happens:

``````if (n < 1) { return [] } //this doesn't run because n = 2
else {
const  countArray = countdown(n-1)
}
``````

Right there, before the rest of the function can run, we have to evaluate countdown(2-1) and assign its value to to the local countArray variable. So now we do countDown(1-1).

``````//n is still isn't less than one, so we don't run the code in the conditional.
else { countArray = countdown(1-1) //once again, we must evaluate countdown(1-1) before we can continue the rest of the function.
``````

So now we evaluate countdown(1-1) or countdown(0)

``````if (n < 1) { return [] } //this finally runs since n is less than 1
``````

Cool. We got our return value from countdown(0), which is an empty array, which will be assigned to the countArray variable in countdown(1). Now we can finish running the code in countdown(1)

``````countArray.unshift(n) //places n(which is 1) into the start of the array.
return countArray //returns the value  from countdown(1).
``````

Remember that countdown(2-1), or countdown(1) was just being called by countdown(2) so that we could assign its value to countArray in countdown(2). So, in countdown(2), here’s what happens.

``````countArray = countdown(n-1) //countArray is now 
countArray.unshift(n) //n = 2, so countArray places 2 at the beginning of the array.
return countArray //returns [2,1]
``````

To sum things up, the function will keep continue to call itself until it finally returns a value that isn’t a function call. When this happens, it backtracks through all of the calls it made until its done. That’s why you get the empty array first.