I have a function which calculates taxes.
function taxes(tax, taxWage)
{
var minWage = firstTier; //defined as a global variable
if (taxWage > minWage)
{
\\calculates tax recursively calling two other functions difference() and taxStep()
tax = tax + difference(taxWage) * taxStep(taxWage);
var newSalary = taxWage - difference(taxWage);
taxes(tax, newSalary);
}
else
{
returnTax = tax + taxWage * taxStep(taxWage);
return returnTax;
}
}
I can't see why it doesn't stop the recursion.
In this arm of your function:
you are not returning a value from the function or setting
returnTax
. When you don't return anything, the return value isundefined
.Perhaps, you want this:
There is a bug with your recursion:
You don't return anything when the condition in the
if
evaluates to true. You need to change that to:You have the necessary
return
statement in theelse
.