Right now, I’m on another exercise of Javascript. And I took the liberty of clicking on I need a hint. As of now here is my code.
// Only change code below this line
function countdown(n){
if (n < 1){
return [];
}
else{
const 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/83.0.4103.34 Safari/537.36.
Challenge: Use Recursion to Create a Countdown
Link to the challenge:
I do not understand why the value of ‘n’, being placed after the previous value of ‘n’ via ‘unshift’, isn’t unshift supposed to place a new value ‘infront’ of the array? If so, why are they being placed after the previous value of ‘n’ in the array? @_@
Ah, I understand, that’s why it’s happening like that. It seems that I’ll have to repeat the lessons again, since I’m still finding it hard to understand the concepts,
The thing that takes a while to switch your head over to is that recursion is not a loop.
You’re used to solving numbers with loops, where you repeat a sequence of commands within a set context. Recursion is not a looping function. Every time a function gets called, it is called in isolation. It does not carry context with it. It doesn’t “know” whether it was called recursively or not. Multiple instances of the function are running separately.