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 {
   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:

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.


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

function returnLocal(arg) {
  var myLocal = [1];
  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: