I have been having some troubles with recursion and can not get it still. I have some doubts:
-
I can not fully understand what makes the function repeat untill it gets to the base case, I think it’s because of the function is inside the function itself but can not get it completely.
-
In this challenge that I am right now, I have some issues understanding how the example given turns to be right, because of: a) when I think that a person set “n” to 5 and put it into the formula I get: countArray = countup(5 - 1) = countArray = 4, and if we push that at the end of countArray (countArray.push(n) ), we would get countArrat = [4, 5]. Then if the function repeats, n will be still being 5, so it would return the second time [4, 4, 5] and so on (in my head haha).
-
I have another issue, in the case of above, when we are given already countArray = [4,5] and the function repeats and excecutes countArray = countup(n - 1), in that moment, would not [4, 5] be errased and be replaced by countup(n = 1). What I mean, doesnt the = makes countArray ONLY be what the last statement say?
Thank you all very much, although I can solve the ecercises given in these challenges, by coping the examples given, I am having so much troubles with this topic and know that if I wanted to make a recursion on a real case escenario, I could not.
**Your code so far**
// Only change code below this line
function countdown(n){
if (n < 1) {
return [];
} else {
let countArray = countdown(n - 1);
countArray.unshift(n);
return countArray;
}
}
// Only change code above this line
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36
Challenge: Use Recursion to Create a Countdown
Link to the challenge: