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 ndecreases , but the values in the final array are increasing . This happens because the push happens last, after the recursive call has returned."
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.
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
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.
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
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.