Tell us what’s happening:
Am I missing something? Why is this not an acceptable recursive function?
**Your code so far**
// Only change code below this line
let arr = [];
function countdown(n){
if (n < 1){
return arr;
} else {
arr.push(n);
countdown(n - 1);
return arr.push(n);;
}
}
// Only change code above this line
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36
Also, you are jamming everything into a global variable instead of using the return value of the recursive function call. This means you can only run your function once, since the global variable fills up with past data.
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