I am in a situation where I have a loop that is calling a function. The function will make recursive calls to itself once called.
Is there a way that I can scope a variable to the function and the chain of recursive calls generated from the first call?
Something like this:
for(var i=0;i<100;i++)
{
myFunction();
}
function myFunction()
{
var someNumber = 200;
someNumber -= 10;
if( someNumber > 0)
{
myFunction();
}
}
where at the second iteration of the first call to someNumber would be 190 and not 200. Is this possible to accomplish?
If there is anything confusing here, please let me know.
Yes, use an inner function to do the recursion:
Note, you have a syntax error. Your
whileneeds to befor.Edit: Perhaps your real code is different and calls for a recursive function, but your example would be much simpler by just using a loop: