Use Recursion to Create Countdown

Tell us what’s happening:
I think my function works fine (I’ve checked it with console.log), but Run The Tests button won’t let me go to next lesson.

Your code so far


// Only change code below this line

  var myArr = []
function countdown(n){
  if (n < 1){
  return myArr
} else {
  myArr.push(n)
  return countdown (n - 1)
}
return myArr
}
// Only change code above this line

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36.

Challenge: Use Recursion to Create a Countdown

Link to the challenge:

[I edited my post, which initially proposed an incorrect solution]

I think the idea is to use an array that is not pre-defined outside of the function, similar to the countup() function defined as the example.

In countup(), the push() method appends the item to the end of the array. For countdown(), how might you put an item in the beginning of the array?

When I run your code it passed all the tests, however in my opinion there are small flaws.

  1. return myArr is unreachable, it will never be executed.
  2. Just like @amrlearn said:

This problem can be best illustrated if you run your function twice, for example:

console.log(countdown(5));
// → [ 5, 4, 3, 2, 1 ]
console.log(countdown(5));
// → [ 5, 4, 3, 2, 1, 5, 4, 3, 2, 1 ]

First part of the previous example is okay for this challenge as well and I wouldn’t change it.

if (n < 1) {
  return [];
} else {
  return ...
}

Instead, think about how can you join two arrays in else part of your if statement.

Okay, I understand it but I have a question about an example in this lesson:

function countup(n) {
  if (n < 1) {
    return [];
  } else {
    const countArray = countup(n - 1);
    countArray.push(n);
    return countArray;
  }
}

If countArray.push(n) is executed in every recursive call, or is it executed only once in the end?

It will be executed each time but only after all the recursive calls are done.

For countup(3) for example:

1. const countArray = countup(3 - 1);
  // First it recursively call itself
  2. const countArray = countup(2 - 1);
    // Again
    3. const countArray = countup(1 - 1)
      // Here however n === 0 so it reaches the base case and 
      // returns empty array
      
      // So we know that countup(1 - 1) is empty array
      // Therefore after pushing item
      countArray.push(1);
      // It returns
      return [1];

    // Function can continue to execute
  2. countArray = [1];
     countArray.push(2);
     return [1, 2];
// And finally
1. countArray = [1,2];
   countArray.push(3);
   return [1,2,3];

I’d rather use pen and paper to explain this but I hope I shed some light on this :wink:

Okay so every time code reaches this line:

const countArray = countup(n - 1);

every next value is added to countArray
When function reaches a base case, it adds square brackets to countArray to be an array.
Then code goes to countArray.push(n) line and pushes every value to countArray. Is it something like that? :smiley:

1 Like

Your code contains global variables that are changed each time the function is run. This means that after each test completes, subsequent tests start with the previous value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

var myGlobal = [1];
function returnGlobal(arg) {
  myGlobal.push(arg);
  return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
  var myLocal = [1];
  myLocal.push(arg);
  return myLocal;
} // reliable - always returns an array of length 2

When function reaches a base it returns an empty array. Then values from each call are pushed to this array. If this is what you mean, then yes :wink:

Some time ago I found this lecture from cs50 very helpful to understand recursion.