Why this recursive solution wrong?

Tell us what’s happening:
Describe your issue in detail here.

   **Your code so far**

// Only change code below this line
function countdown(n){
if(n<1) {
  return [];
}
return countdown(n-1).unshift(n);

}
// Only change code above this line
   **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36

Challenge: Use Recursion to Create a Countdown

Link to the challenge:

I don’t think unshift is returning what you think it does. Try running this piece of code.

console.log([1,2].unshift(3))

That code returns 3. Why is that? I thought unshift puts stuff in an array. Thank you for the reply btw

It adds an item to the beginning of the array, then returns that item (not the whole array).

2 Likes

Thank you sir now I know what the issue is.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.