Basic JavaScript: Use Recursion to Create a Countdown with unshift

Tell us what’s happening:
why is my code wrong? I tried to use it on Scrimba and it worked fine. But here it said it fails to do these while the code could do so on Scrimba (
After calling countdown(myArray, 10) , myArray should contain [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

After calling countdown(myArray, 5) , myArray should contain [5, 4, 3, 2, 1])

Your code so far


//Only change code below this line
var myArray = [];
function countdown(myArray, n){
  
if (n === 1) {
  return [1]
} else if (n > 1) {
  var arr = countdown(myArray, n - 1);
  arr.unshift(n);
  return arr;
}

}

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 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

I don’t understand, why did you make another function ‘padArray’ to return countdown when it can be done (return the value) in the countdown function?
Plus, the problem is that I don’t understand where does my code go wrong when it can run just fine on Scrimba, why it is not in here?

That is just how we wrote the tests.

Plus, the problem is that I don’t understand where does my code go wrong when it can run just fine on Scrimba, why it is not in here?

If you add the padArray function I posted above to your solution and call it with the following to see what it returns, you will see that the array it returns is not [10, 9, 8, 7, 6, 5, 4, 3, 2, 1].

console.log(padArray([], 10);

Your countdown function does not need to return a value at all. It just needs to modify the array. Your current countdown function is creating a new array (arr) and making changes to it instead of modifying the original array myArray. That is why after your function completes, myArray is still an empty array []. You will notice that no where in the instructions does it say your function should return a value. It only says it should append the numbers to the myArray variable.