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
Thanks for your answer. That makes total sense, but just changing it to local hasn´t solved the problem. The tests run all in one execution and the global variable is carrying the previous data.
I have used the same approach as in the example, but it is not very intuitive to me.
only one number is added to count before it is returned, and at each function call you do nothing with the value returned from countdown(n-1), but that’s the point of recursion, doing something with the returned value