Tell us what’s happening: I’m having trouble fully understanding the else block. I understand that the recursion that’s happening with the countdown(n - 1);
is counting down until it hits the base case which then returns the empty array after and moves on to the else block again to the last 2 lines within the else block. When I use the debugging tool in VS code on these 2 lines, the countArray.unshift(n);
line is showing that is counting up from 1 - 10 and after each time it returns a number starting with 1 since it’s the current value of n
, it applies that number to the countdown function and is somehow counting up while unshifting the numbers to the front of the indexed positions. That’s exactly what I don’t understand. How is it counting up but, and printing the numbers in a countdown? I hope that makes sense.
Describe your issue in detail here.
Your code so far
javascript
// Only change code below this line
function countdown(n){
if (n < 1) {
return [];
}
else {
const countArray = countdown(n - 1);
countArray.unshift(n);
return countArray;
}
}
console.log(countdown(10));
// 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/120.0.0.0 Safari/537.36
Challenge Information:
Basic JavaScript - Use Recursion to Create a Countdown