Basic JavaScript: Use Recursion to Create a Countdown Andrej

Code :

so, step 1, countup(5) is called, inside there there is const countArray = countup(n -1); , so now countup(4) is called, there is again that line, countup(3) is called, there is again that line, countup(2) is called, there is again that line, countup(1) is called, there is again that line, countup(0) is called - this time, with n < 1 being true, the line executed is return [] , so now we have that countup(0) resolve as [] , so for the line const countArray = countup(0) , the value of countArray is [] . Now that countArray has a value the next line can be executed, at this point n has value of 1 , so we have countArray.push(1) , and countArray becomes [1] . And that value is returned. At this point finally also countup(1) has an output. And so on.

I get everything except how after you get [1] you get the 2 and then the 3. I dont see how you add one to n from here.


When you only post a link to the challenge, it shows us our own code not yours. We need to see your code to help.
The easiest way to do so is to click the “get help” button on the challenge, then click “Ask for help”. That will generate a forum post that includes your code.
Alternatively, you can reply to this post with your code with a line of 3 backticks (```) before and after your code block.

I dont have any code yet I am trying to understand the example.

‘’’ function countup(n) {
if (n < 1) {
return ;
} else {
const countArray = countup(n - 1);
return countArray;
console.log(countup(5)); // [ 1, 2, 3, 4, 5 ] ‘’’

And why it returns [1,2,3,etc.] and not [5,4,3,etc].

I understand the concept that the function runs until the base case is reached and an empty array is created, but I dont understand how n then grows from 1 to 5

Let’s see if this graph I made can help you:


1 Like

Yes that graph does help alot, I’m just confused about how the function can work backwards and save all the values of countArray which it then pushes into the array.

Hi.!!! This video may help you to understand how the whole recursion concept works. It was a bit confusing for me as well initially.
Uderstanding recursion

1 Like

just look at first box:
countup(4) will return a value (and to do that there is all the stuff to the right), but once the function returns a value that value is stored inside countup(5) in the value countArray, after that a new value is added at the end of it, and the array is returned as output of countup(5)

each function stay in the memory pending until all the other stuff opened inside it will finish computing. (they are all different function calls)

I thought that the function runs until the base case is reached, and then starts pushing values from there.

Sorry this is just really hard for me to wrap my head around for some reason

I think the youtube video explained it for me

1 Like

recursion is by definition a function calling itself again and again, but each function call is a new function call

the function stops calling itself again and again when the arguments trigger the base case, then each function starts getting resolved

1 Like