# Understanding Recursion and Countups

This is the example from the recursion lesson. I don’t understand why the example returns [1,2,3,4,5] if n=5. My thought process is countArray will equal [n-1, (n-1)-1, ((n-1)-1)-1, …] So if n=5 it should return 4,3,2,1 and then push 5 to the end. Final result is [4,3,2,1,5]. Can someone please tell me the error in my thought process? Much appreciated!

``````  **Your code so far**
``````
``````// Only change code below this line
function countup(n) {
if (n < 1) {
return [];
} else {
const countArray = countup(n - 1);
countArray.push(n);
return countArray;
}
}
console.log(countup(5));
// Only change code above this line
console.log(countdown(5));
``````
``````  **Your browser information:**
``````

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

Challenge: Use Recursion to Create a Countdown

This question and ones like it have been asked many times, please search the forum. There explanations of this exact function, some of them quite detailed.

Keep in mind that recursion is a confusing subject, especially for beginners. (Heck, it still warps my brain a little.) Don’t expect to get it overnight.

I also always recommend learning how the call stack works as that is very tied with recursion, and maybe look up some videos because some of this can be visual.

If after all of that you still have questions, check back.

Can someone please tell me the error in my thought process? Much appreciated!

More specifically, you are misunderstanding the order in which functions are starting and ending. (Which is very, very, very common for people first learning this.) I might suggest riddling your function with log statements and see what happens.

Don’t feel bad, being confused by recursion is very normal.

2 Likes

I looked at some other examples with a chart and looked at the how a call stack works and this is my understanding: First the function evaluates if n < 1, if so then it returns . If not it creates a local variable countArray that equals n-1. It then pushes N to the end of the array and returns that array. Because we called the function inside itself the function becomes recursive and will repeat until it reaches the base case. So if N = 5 the first time it runs the array becomes 5 the second 5,4 third 5,4, 3 fourth 5,4,3,2 and fifth 5,4,3,2,1. (It stops after this as the base case as been reached) But this is still wrong b/c the answer should be reverse [1,2,3,4,5]. Also, in the lesson it says the push happens last after the recursive call has returned. This confuses me as I though recursion repeats the entire function.

i will share a post that helped me and hope it’s relevant:

1 Like

That helped so much thank you!

1 Like

``````function countup(n) {
console.log('* enter function, n =', n)
if (n < 1) {
console.log('** base case reached, returning []')
return [];
} else {
console.log('not base case, n =', n)
const countArray = countup(n - 1);
console.log('countArray =', countArray, 'and n =', n)
countArray.push(n);
console.log('** returning countArray =', countArray)
return countArray;
}
}