Basic JavaScript - Use Recursion to Create a Countdown

THE TASK The function should use recursion to return an array containing the integers n through 1 based on the n parameter. If the function is called with a number less than 1, the function should return an empty array.

Im struggling with recursion and hoping for some clarification. If I remove this line countArray.splice(0, 0, n), then the log returns an empty array. Which would suggest that n < 0, which is obviously not true here.

I feel like let countArray = countdown(n - 1); should return [1, 2, 3, 4, 5] here, when the line countArray.splice(0, 0, n) is removed.

If that’s true, then why is it returning an empty array and how does adding countArray.splice(0, 0, n) OR countArray.unshift(n) invert the value of let countArray = countdown(n - 1);?

// Only change code below this line
function countdown(n){
  if (n < 1) {
    return [];
  } else {
    let countArray = countdown(n - 1);
    countArray.splice(0, 0, n)
    return countArray;
  }
}
// Only change code above this line


console.log(countdown(5));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:107.0) Gecko/20100101 Firefox/107.0

Challenge: Basic JavaScript - Use Recursion to Create a Countdown

Link to the challenge:

Why would it do that? You never put anything in the returned array if you delete the splice.

If you never insert anything into the result array, then the result array will stay empty.

It doesn’t. Unshift or splice is only adding one value.

This is a function call. It must be resolved to a return value before the next line is executed. When n === 5, what should the result of this function call be?

My mistake. I honestly keep forgetting that you don’t want the answer from me.

He is trying to help you. You can learn a lot from him if you wish to.

1 Like

I find that highly debatable in my experience.

Hey there, this is just a quick reminder that we want to keep this space safe and welcoming for all. Please keep your interactions with your fellow campers friendly and positive.

1 Like

It returns an empty array because you are never adding an actual number to countArray when you remove that line.

Try a simple example:

countdown(1);

When this executes it hits the else statement and does:

let countArray = countdown(n - 1);

Which is actually:

let countArray = countdown(0);

And you know that countdown(0) returns an empty array because that’s your base case. So the first line of the else statement is:

let countArray = [];

So if you don’t have the splice line after that then 1 never gets added to countArray and you are just returning an empty array.

1 Like

Its taken me a few days but I finally grasped what it is you’re saying and what’s going on here. But what I dont understand is, why cant I use .push here? Why is push returning [1, 2, 3, 4, 5] instead of [5, 4, 3, 2, 1]?

And why is this test failing: Global variables should not be used to cache the array - when the only thing I change is .push instead of splice or unshift? Im really loathing recursion.

Try and use some pen and paper to write down the function calls in order to understand how the result is being formed
(Assuming that you have a push to add n)

To start for eg you would write

countdown(5)
  countdown(4)
    countdown(3)
      countdown(2)
        countdown(1)
          countdown(0)
          -> returns []
        [].push(1) 
        -> returns [1]
      [1].push(2)
      -> returns [1, 2]

Try to finish writing this and you will see why push doesn’t actually countdown.

1 Like

Dont push and unshift do the exact same thing, with the only difference being where one starts adding the data (beginning or end)? So if we are pushing or unshifting to an empty array, shouldn’t the results be the same? Since the end and beginning are the same?

They are not the same. Unshift will add to the start of the array, so the sequence of events will be something like


[1]
[2, 1]
[3,2,1]
Etc.

(Very important to check the docs when we use predefined functions because sometimes not doing so first can lead to a lot of wasted time in debugging)

2 Likes

Everything makes sense now, thank you.

1 Like

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