Recursion Countdown keeps failing

Tell us what’s happening:
Hello everyone, could somebody explain why my code doesnt pass the test? Thank you in advance and Happy New Year !

My code so far


//Only change code below this line

let arr=[];

function countdown(n){
if (n<=0) {
  return [];
} else {
   arr.push(n);
   return countdown(n-1), arr;
}
}



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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 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

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
1 Like

Yes agreed with @ilenia If I want to put in other way such you can understand that whenever you run the function countdown(10) the array arr becomes [10,9,8,7,6,5,4,3,2,1]. So if you would again run this function countdown(10) this would return [10,9,8,7,6,5,4,3,2,1,10,9,8,7,6,5,4,3,2,1] which would not pass the test. So put it simply your function should return same value on same input. Hope this helps :grinning: