Basic JavaScript - Use Recursion to Create a Countdown

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

I wrote this code without any assistance and it runs perfectly. But i honestly just patterned it after the example in the exercise, by my logic the console.log should be returning the reverse of the countdown i.e 1,2,3,4,5 and i should not be passing this exercise.

Here’s how i see it -

The first " countArray.unshift(n) " adds the intial value of n into the beginning of the array … lets say n = 5.
[5, …]

next time the loop runs " countArray.unshift(n) " should add 4 to the beginning of the array.

[ 4, 5, …]

until we get 1,2,3,4,5 instead i get 5,4,3,2,1 .

can someone dumb down what is happening here for me ?
Your code so far

function countdown(n){
if (n < 1){
return ;
} else {
const countArray = countdown(n - 1);
countArray.unshift(n);
return countArray;
}
}
console.log(countdown(5));

// 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(5));
// 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/103.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Use Recursion to Create a Countdown

Link to the challenge:

Yes, it does, but what happens on the line just before that?

1 Like

We assign the value of the function “countdown” -1 to the variable called “countArray”.

Its not clear to me why that would even give us an array in decreasing order, as each decreasing value of n is ultimately shifted to the beginning of the array.

We assign the results of the function call countdown(n-1) to the variable countArray.

Yes, but the important point to realize is when that happens. If you call the function as:

countdown(5);

You are correct, the 5 is ultimately going to be added to the beginning of the countArray. But not until you do the following:

const countArray = countdown(n - 1);

You can’t just write this line off. It does something. It returns a value. And then you are adding 5 to the value it returned.

So don’t start with countdown(5), that’s too many numbers. Let’s start with countdown(2). When you first call it, you hit the else. You can’t add 2 to countArray until the line above it returns a value which is assigned to countArray. Start plugging in the values. If we call your function as:

countdown(2);

Fill in the values for:

const countArray = countdown(n - 1);

What is n - 1. What does countdown(n-1) return? You have to do that before you can add 2 to the beginning of it.

2 Likes

I worry that i’m not following this properly, here’s my understanding of your explanation.

In a scenario where n is equal to 2.
// we hit the else statement -

const countArray = countdown(n - 1);

// n = 2, but before we can “push” n to “countArray” the function “countdown (1)” has to run and we will store the result of that in countArray.

//countdown(1) runs and also hits the “else statement” and we’d be at (1-1).

At this point the logic falls apart in my head because then we’d be running countdown(0) which would return an empty array.

That’s exactly right. So what does countdown(1) return? First you do:

const countArray = countdown(0);

You said yourself that countdown(0) returns an empty array, so that means that countArray will be set to an empty array. Then we can do the next line:

countArray.unshift(1);

So countArray now equals [1] and that’s what will be returned for countdown(1).

So now you know what countdown(1) returns. Let’s go back to countdown(2). The first thing we do is:

const countArray = countdown(1);

But you know what countdown(1) returns, right? So we can replace it with:

const countArray = [1];

And then we go on to the next line:

countArray.unshift(2);

Which makes countArray equal to [2, 1].

1 Like

Ouuuuuuuu !
I’ve got tears in my eyes, this makes so much sense.
Thank you so much !

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