Please see my code below for recursion countdown. I see the output coming correctly in the console log. But it is not passing the tests for countdown(10) and countdown(5).
Appreciate your help.
**Your code so far**
// Only change code below this line
var countArray = [];
function countdown(n){
if (n < 1) {
return [];
} else {
countArray.push(n);
countdown(n-1);
return countArray;
}
}
console.log(countdown(10));
// Only change code above this line
**Your browser information:**
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.3 Safari/605.1.15.
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