递归函数返回undefined [英] Recursive function returns undefined
本文介绍了递归函数返回undefined的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个计算税收的功能。
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.
推荐答案
在你的职能部门:
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);
}
您没有从函数或设置中返回值 returnTax
。当你没有返回任何东西时,返回值是 undefined
。
you are not returning a value from the function or setting returnTax
. When you don't return anything, the return value is 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);
}
这篇关于递归函数返回undefined的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文