Use Recursion to Create a Countdown - Why not the reverse order?

I really don’t understand how come the .push() method is adding numbers in the array this way [ 1, 2, 3, 4, 5 ]. It looks like .unshift() to me.

.push() is supposed to simply add the numbers at the end. If so, in my understanding, the array should look like [5, 4, 3, 2, 1] because n = 5. I really can’t wrap my head around it. Someone please explain…

I appreciate anyone’s help big time. cheers.

The explanation on the challenge page shown below doesn’t really explain anything to me.

" At first, this seems counterintuitive since the value of `n` decreases , but the values in the final array are increasing . This happens because the push happens last, after the recursive call has returned."

Challenge: Use Recursion to Create a Countdown

3 Likes

Hello @Salix.Koreensis,

• Next time you want to showcase a code, use the ``` backtick so it is easier for everyone to read.
See this post to find the backtick on your keyboard.
The “preformatted text” tool in the editor (`</>`) will also add backticks around text.
• `.push()` has a big difference than `.unshift()`. `.push()` adds new item into the LAST of the array. In the other hand, `.unshift()` adds new item to the BEGINNING.
• Example:
``````//.push()
let arr1 = [1, 2, 3, 4, 5];
arr1.push(6);
console.log(arr1);//outputs [1, 2, 3, 4, 5, 6]

//.unshift()
let arr2 = [1, 2, 3, 4, 5];
arr2.unshift(6);
console.log(arr2);//outputs [6, 1, 2, 3, 4, 5]``````
1 Like

I have created this graph to show what happens to the function:

when countup(5) is called you have inside it the part

``````const countArray = countup(4);
countArray.push(5);
return countArray;
``````

in short
by definition, countup(4) will return [1,2,3,4], so then the push add the 5 at the end of the array.

13 Likes

Hi ieahlaeen,

I’m starting to see how this code works… so basically

``````return countArray;
``````

in else won’t work until

``````return [];
``````

is hit. And then the numbers are pushed in the order closest to the return[]; and backward.

My head is still working real slow on this but I feel like I’ll soon walk out of the fog.

Thank you so much for your time ieahleen!
I really appreciate that you spent your precious time for this thread.

4 Likes

for a more complete explanation you can also go to “Get Help->Get a Hint” to see the guide on this challenge, all the solutions are hidden by default so you can look at the whole explanation without worrying of accidentally seeing one

1 Like

Thanks ieahleen for the explanation , and salix for asking!! . Had a difficult time to understand why its adding and returning array in increasing order. I thought for long time to understand why the code did not stop and come out of main function after pushing [1] , so while returning blue direction for const countArray = countup(1) its actually setting countArray to [1] and hence it is able to jump to next line and push 2 to countArray !! right? How can it remember the value of n while coming back in blue dir ? is it like being dumped in background as in your flow chart and hence able to comeback to reach countup(5) ? … Sry for the long Q.

a function stays in memory until it’s completed.

that’s why when you don’t give a stopping case for your recursion you get a `Stack Overflow` error, unfinished functions start piling up, too many functions in memory and the browser can’t deal with it and just stops and error out

2 Likes

Yes, got it now, thank you so much…

More Different Solutions to this exercise

``````function countdown(n) {
const arr = [];
function downward(n){
if (n<1)
return [];
else
arr.push(n);
downward(n-1)
}
downward(n)
return arr;
}
``````
``````function countdown(n) {
if (n < 1) {
return [];
} else {
const countArray = countdown(n - 1);
countArray.push(n);
countArray.sort(function(a, b) {
return b - a;});
return countArray;
}
}
``````
``````var total = []
function countdown(n) {
if (n>=1) {
total.push(n)
countdown(n-1)
} else {
return [];
}
}
``````

the last one is not correct

Your code contains global variables that are changed each time the function is run. This means that after each test completes, subsequent tests start with the previous value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

``````var myGlobal = [1];
function returnGlobal(arg) {
myGlobal.push(arg);
return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
var myLocal = [1];
myLocal.push(arg);
return myLocal;
} // reliable - always returns an array of length 2
``````

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with `[spoiler]` and `[/spoiler]` tags on the line above and below your solution code.

Thank you.