There is a piece of recursive function that I was working on and it seems to be returning the wrong value than what is expected. I was able to replicate it and simplify the function into:
function foo(i, target){
if(i < target){
i++;
foo(i, target);
}
return i;
}
console.log(foo(0,5));
Basically, given the function above, I expect the return value to be 5. However, it seems to return 1. When I did some tracing, I notice that "return i" is being called several times and each time it gets decremented with 1? What's the reason for this behaviour and what can I do to fix it?