I don’t understand why my results are not validated, by logging the results in the console it seems that everything is working fine:
**Your code so far**
// Only change code below this line
let arr = [];
function countdown(n){
if(n<=0){
//console.log(arr);
//console.log(typeof arr );
return arr;
} else {
arr.push(n);
countdown(n-1);
}
}
// Only change code above this line
**Your browser information:**
User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0
Challenge: 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 function call completes, subsequent function calls 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
Thank you for the suggestion, but even when I define a new function in which I initialize the variable the same problems occur. The thing I don’t understand is why my output is the same as the expected output but the exercise validation fails.
modified code:
// Only change code below this line
function countdown(n){
let arr = [];
function recursiveFn(n){
if(n<=0){
console.log(arr);
//console.log(typeof arr );
return arr;}
else {
arr.push(n);
n--;
recursiveFn(n);
}
}
recursiveFn(n);
}
// Only change code above this line