Use recursion to create a countdown. passed but didn't get it

Tell us what’s happening:

can anyone please tell me how this line of the code const arr = countdown(n - 1) return an array? and how does this work?

Your code so far


//Only change code below this line
function countdown(n){
if (n < 1) {
  return [];
} else {
  const arr = countdown(n - 1)
  arr.unshift(n)
  return arr
}
}
console.log(countdown(5)); // [5, 4, 3, 2, 1]

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36.

Challenge: Use Recursion to Create a Countdown

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/use-recursion-to-create-a-countdown

1 Like

I’ll give it a stab. Let’s see…

So we call the function countdown(5), and the first thing it checks is, is n less than one? If SO, create an empty array. In this case, it’s not, so it drops into the else section, and calls countdown(4), and assigns the return value to the variable arr. This part is key.

Now, with countdown(4), n=4 - so less than one? Nope. So drop to the else, and call countdown(3). Do it again, countdown(2). Again, countdown(1).

Finally, we call countdown(0). At this point, n=0. Is this less than one? Yup. So… we return an empty array. Where do we return that empty array? To the function just before this one - the function call countdown(1).

So now we’re unwrapping our recursive calls. Inside countdown(1) now, arr is an empty array, and n=1. The next thing we do here is arr.unshift(1), prepending n to that empty array, and returning arr (which is now [1]) to the next layer out - the function call countdown(2).

At this point, arr=[1] and n=2. Remember, we’re working our way back out of a whole bunch of nested function calls. So, still in the else branch of countdown(2), we arr.unshift(n), where n=2, so arr=[2,1], and return that value to countdown(3), which arr.unshifts(3), causing its own internal arr to be [3,2,1].

And we KEEP doing this, till we get back to our outermost function call, countdown(5). At that point, we’ve gone from an empty array, to an array with a single value, prepending and prepending, till we get to arr=[4,3,2,1] and n=5.

At this point, we do it one last time, making arr=[5,4,3,2,1], and we return that to our console.log(countdown(5) ) statement, displaying that final array.

Does that clear things up at all?

7 Likes

So that thing happened because I returning an empty array at the base case. I mean because of this. if (n < 1) { return [];. right?

2 Likes

Exactly. Because the INNERMOST recursion returns an empty array, as we unpack each of the recursive calls, un-pausing each recursive step in turn, we build on that array.

1 Like

So when I remove return [] and code like this

function countdown (n) {
  if (n < 1) {
    return 0;
  } else {
    return countdown (n - 1)
}

console.log(countdown(5))

can you please tell me why am I getting 0 in return?

I know it works if I log in console n before return countdown(n - 1). and returns desired countdown. I just don’t know why?

I think I understood what you teach me about the “ARRAY PART”. it’s a new problem I’m facing.

1 Like

Thank you very much. this app is amazing. hope it will help me a lot.

Thank you so much @snowmonkey you flawlessly cleared up my confusion as to why the empty array was being returned when if (n < 1)

1 Like

This helped me understand this a bit. thank you

This is the app I needed to understand many things. Thank you for that!

OMG. I would have never figured this out on my own. Thank you very much! And thanks to everyone who takes time in these forums to explain things. Returning an empty array when the base case was met, and using unshift were things I just couldn’t come up with on my own from the problem description alone.

1 Like

Here’s a bit longer explanation, sort of unpacks the mindset behind recursion. We use it every day, we just don’t realize we are. This particular conversation is “using recursion to calculate an Nth-power.”