Tell us what’s happening:
Hello,
I’m quite confused about the principle of how the function is working here.
Can someone please explain how the line of code as const countArray = countup(n - 1);
creates an array with necessary numbers in the correct order?
Your code so far
function countup(n) {
if (n < 1) {
return [];
} else {
const countArray = countup(n - 1);
countArray.push(n);
return countArray;
}
}
console.log(countup(5)); // [ 1, 2, 3, 4, 5 ]
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36
.
Challenge: Use Recursion to Create a Countdown
Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/use-recursion-to-create-a-countdown
There have been some really good conversations about this on the forum. Here are just a couple.
Ok, my problem isn’t passing the challenge. My problem is understanding why the code in the challenge example actually does what it does. I don’t want to write code I don’t understand just to pass the challenge and move forward. I want to actually understand what is going on behind the scenes so to speak.
I left a few comments on the code below to help outline my confusion. I tried writing things out on paper but that got confusing. It seems to me that countArray doesn’t actually become an arra…
Tell us what’s happening:
Code works fine. There are no problems with execution or whatsoever.
I just cannot understand how this recursion works, on this block:
else {
const countArray = countdown(n - 1);
console.log(`n: ${n}, countArray ${countArray}`);
countArray.unshift(n);
return countArray;
The first line that is getting executed is:
const countArray = countdown (n - 1);
which as far as I can tell this the point that the variable recalls the function and as I far as I know, w…
1 Like
ilenia
March 5, 2020, 6:06pm
3
there has just been a post opened on this same thing
Could you take a look there (and at the threads linked in there) and see if it clear some ideas for you?
In this example:
function countup(n) {
if (n < 1) {
return [];
} else {
const countArray = countup(n - 1);
countArray.push(n);
return countArray;
}
}
console.log(countup(5)); // [ 1, 2, 3, 4, 5 ]
I can’t understand how this function is implemented (the execution flow)
i want to know how the -n- variable is incremented from 1 to 5 !!
i understand the definition of the recursion but still wonder about the variable inside this function , maybe little info about how it work…
If it is not enough feel free to ask again! (Here, or in the thread where the post you want some clarifications on was written)
1 Like
Thank you!
So, for the countup function, we are creating a const rather than declaring var is because, in this case, it stands for the number that gets pushed to the stack and it is the value they won’t be modified, that would be correct?
Any time that we aren’t going to reassign a variable, it’s best practice to use const
.
2 Likes