Tell us what’s happening:
Hi Everyone,
I don’t understand how the function is able to unshift the consecutive values of N into the array if the recursive call must be evaluated to the point when n < 1 before it is completed.
If I put n = 6, does the function evaluate at n=5 and then unshift the value 5 and then return myArray[5] before going back to evaluate n =4?
Your code so far
// Only change code below this line
var myArray = [];
function countdown(n){
if (n < 1){
return [];
}
myArray = countdown(n-1);
myArray.unshift(n)
return myArray
}
// Only change code above this line
console.log(countdown(6));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0.
So the code passed in each value of n until the base case, then it continued until it reached the return. I’m going to play around with the base case to see what the array looks like after.
Thank you for the reply it really cleared up what was going on .