递归功能返回未定义

发布于 2025-02-13 09:32:43 字数 546 浏览 2 评论 0原文

我的功能可以计算税收。

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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

短叹 2025-02-20 09:32:43

在您的功能的此部门中:

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); 
}

您没有从功能或设置return tax中返回值。当您不返回任何内容时,返回值为undefined

也许,您想要这个:

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); 
}

In this arm of your function:

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); 
}

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:

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); 
}
红颜悴 2025-02-20 09:32:43

递归中有一个错误:

taxes(tax, newSalary);

如果在true中评估中的条件时,您不会返回任何内容。您需要将其更改为:

return taxes(tax, newSalary);

您在else> else中具有必要的返回语句。

There is a bug with your recursion:

taxes(tax, newSalary);

You don't return anything when the condition in the if evaluates to true. You need to change that to:

return taxes(tax, newSalary);

You have the necessary return statement in the else.

鲜血染红嫁衣 2025-02-20 09:32:43

例如税收(税,新闻);返回100;

taxes(tax, newSalary); // undefined

您期望看到100,因为您递归地称呼税收(税,新闻),但实际上您获得了值(100),您需要返回此值。

return taxes(tax, newSalary); // 100
// simply it's the same as
// return 100
// because taxes(tax, newSalary) returned 100

返回100后,您将获得此值。

For example taxes(tax, newSalary); returns 100;

taxes(tax, newSalary); // undefined

You expect to see 100 because you called taxes(tax, newSalary) recursively but in fact you got the value (100) and you need to return this value.

return taxes(tax, newSalary); // 100
// simply it's the same as
// return 100
// because taxes(tax, newSalary) returned 100

after return 100 you will get this value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文