Home > Web Front-end > JS Tutorial > Why Does My Recursive Tax Calculation Function Return Undefined?

Why Does My Recursive Tax Calculation Function Return Undefined?

Linda Hamilton
Release: 2024-12-10 06:55:09
Original
355 people have browsed it

Why Does My Recursive Tax Calculation Function Return Undefined?

Recursive Function Returns Undefined

The provided function calculates taxes using recursion, but it fails to stop the recursion. This issue arises from not returning a value within the recursive arm of the function.

Within the if (taxWage > minWage) block:

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); 
}
Copy after login

The code calculates taxes recursively but does not return a value or set returnTax. The absence of a return statement results in an undefined return value.

To rectify this, a return statement can be added to this arm:

if (taxWage > minWage) {
    // calculates tax recursively calling two other functions difference() and taxStep() 
    tax = tax + difference(taxWage) * taxStep(taxWage);
    var newSalary = taxWage - difference(taxWage);
    return taxes(tax, newSalary); 
}
Copy after login

With this adjustment, the function will now return a value, preventing the recursion from continuing indefinitely.

The above is the detailed content of Why Does My Recursive Tax Calculation Function Return Undefined?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template