[英]3 returns for one input for a calculator
I am very new to programming so please forgive my perhaps dumb question. 我对编程非常陌生,因此请原谅我可能很愚蠢的问题。 I have built three functions that calculate tax rates based upon the same input "amount".
我建立了三个函数,这些函数根据相同的输入“金额”来计算税率。 I am trying to figure out a way that I could have a user enter in the amount once and get the return from all three functions.
我试图找出一种方法,让用户一次输入金额并从所有三个函数中获得收益。 Here they are below.
它们在下面。
//Function 1
var normalrtfCalculator = function (amount) {
if (amount <= 150000) {
return Math.ceil(amount / 500) * 2;
} else if (amount <= 350000) {
if ((amount - 150000) <= 50000) {
return 600 + (Math.ceil((amount - 150000) / 500) * 3.35);
} else {
return 935 + (Math.ceil((amount - 200000) / 500) * 3.9);
}
} else {
if ((amount - 200000) <= 350000) {
return 2735 + (Math.ceil((amount - 200000) / 500) * 4.8);
} else if ((amount - 550000) <= 300000) {
return 4655 + (Math.ceil((amount - 555000) / 500) * 5.3);
} else if ((amount - 850000) <= 150000) {
return 7835 + (Math.ceil((amount - 850000) / 500) * 5.8);
} else {
return 9575 + (Math.ceil((amount - 1000000) / 500) * 6.05);
}
}
};
//Function 2
var mansionTax = function (amount) {
if (amount > 1000000) {
return amount * 0.01;
}
};
//Function 3
var lowincomertfCalculator = function (amount) {
if (amount <= 350000) {
if (amount <= 150000) {
return (Math.ceil(amount / 500)) * 0.5;
} else {
return 150 + (Math.ceil((amount - 150000) / 500)) * 1.25;
}
} else {
if ((amount - 150000) <= 400000) {
return 420 + (Math.ceil((amount - 150000) / 500) * 2.15);
} else if ((amount - 550000) <= 300000) {
return 2140 + (Math.ceil((amount - 550000) / 500) * 2.65);
} else if ((amount - 850000) <= 150000) {
return 3730 + (Math.ceil((amount - 850000) / 500) * 3.15);
} else {
return 4675 + (Math.ceil((amount - 1000000) / 500) * 3.4);
}
}
};
Just have a function that runs the other ones and returns an object of the results: 只要有一个运行其他函数并返回结果对象的函数:
var calculateTax = function(amount){
return {
rtf : normalrtfCalculator(amount),
mansion: mansionTax(amnount),
lowincome: lowincomertfCalculator(amount)
}
}
then you can call it like this: 那么您可以这样称呼它:
var tax = calculateTax(99999);
To get the individual results you then just access the properties: 要获取单个结果,您只需访问属性:
alert(tax.rtf)
, alert(tax.mansion)
and alert(tax.lowincome)
alert(tax.rtf)
, alert(tax.mansion)
和alert(tax.lowincome)
The minimum change required could be as little as adding ()
before assigning to the variables. 分配给变量之前,所需的最小更改可能只需加上
()
。
Something like : 就像是 :
var amount = 100;
//Function 2
var mansionTax = (function () {
if (amount > 1000000) {
return amount * 0.01;
}
})();
Here is a working sample . 这是一个工作示例 。
This will remove the need for defining any additional functions. 这将消除定义任何其他功能的需要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.