I have a function which calculates taxes.
JavaScript
x
17
17
1
function taxes(tax, taxWage)
2
{
3
var minWage = firstTier; //defined as a global variable
4
if (taxWage > minWage)
5
{
6
//calculates tax recursively calling two other functions difference() and taxStep()
7
tax = tax + difference(taxWage) * taxStep(taxWage);
8
var newSalary = taxWage - difference(taxWage);
9
taxes(tax, newSalary);
10
}
11
else
12
{
13
returnTax = tax + taxWage * taxStep(taxWage);
14
return returnTax;
15
}
16
}
17
I can’t see why it doesn’t stop the recursion.
Advertisement
Answer
In this arm of your function:
JavaScript
1
7
1
if (taxWage > minWage) {
2
// calculates tax recursively calling two other functions difference() and taxStep()
3
tax = tax + difference(taxWage) * taxStep(taxWage);
4
var newSalary = taxWage - difference(taxWage);
5
taxes(tax, newSalary);
6
}
7
you are not returning a value from the function or setting returnTax
. When you don’t return anything, the return value is undefined
.
Perhaps, you want this:
JavaScript
1
7
1
if (taxWage > minWage) {
2
// calculates tax recursively calling two other functions difference() and taxStep()
3
tax = tax + difference(taxWage) * taxStep(taxWage);
4
var newSalary = taxWage - difference(taxWage);
5
return taxes(tax, newSalary);
6
}
7