递归函数返回未定义

编程入门 行业动态 更新时间:2024-10-20 04:03:50
本文介绍了递归函数返回未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个计算税收的函数.

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

更多推荐

递归函数返回未定义

本文发布于:2023-11-28 07:00:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641389.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:递归   函数   未定义

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!