# Help Needed With Exercise : Countdown Using Recursion

Tell us what’s happening:
I’ve written the below code for the countdown by recursion exercise. When I run tests, it tells me that countdown(5) should give me [5,4,3,2,1]. But that’s exactly what the output is. Right below the error, I see [5,4,3,2,1] printed on the console. It tells me countdown(10) should give me [10,9,8,…,2,1]. Again, that’s exactly what I get when I test the function for n=10.

Where am I going wrong?

``````
//Only change code below this line
var myArr = [];
function countdown(n){
if (n<=0) {
return [];
} else {
myArr.push(n);
countdown(n-1);
return myArr;
}
}
console.log(countdown(5)); // [5, 4, 3, 2, 1]

``````

User Agent is: `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.5 Safari/605.1.15`.

Challenge: Use Recursion to Create a Countdown

Your code contains global variables that are changed each time the function is run. It has not a great impact in freecodecamp as each test is executed separately, but you have already a function call in your editor, subsequent tests start with the current value of `myArr`. In Free Code Camp you can just delete the function call in the editor, but you should anyway make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed, as in real projects you have not the protected environment found here.

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