函数结果应该打印以下内容:简单的javascript函数来计算税收
输入您的年龄并单击按钮
答案 0 :(得分:-1)
1-£0到£10k的税率为0%
2-£10,001至£35k的税率为20%
3-£35,001至100,000英镑的税率为40%
4-£100k plus,税率为50%
let income = 75000;
incomeTax = calculateTax(income);
function calculateTax(income) {
let tax = 0;
if (income > 100000) {
tax = .2 * (35000 - 10001) + .4 * (100000 - 35001) + .5 * (income - 100001);
} else if (income > 35000) {
tax = .2 * (35000 - 10001) + .4 * (income - 35001);
} else if (income > 10000) {
tax = .2 * (income - 10001);
}
return tax;
}
答案 1 :(得分:-1)
我可能会这样做:
const brackets = [
{min: 0, max: 10000, rate: 0},
{min: 10000, max: 35000, rate: .2},
{min: 35000, max: 100000, rate: .4},
{min: 100000, rate: .5}
]
const calc = brackets => income => brackets.map(
bracket => (
Math.min(income, bracket.max || Infinity) -
Math.min(income, bracket.min)
) * bracket.rate
).reduce((a, b) => a + b, 0)
const taxes2017 = calc(brackets)
console.log(taxes2017(73500)) //~> 20400
console.log(taxes2017(156300)) //~> 59150
这使您可以更加声明性地列出税表和/或根据不同的表创建不同的税收计算器。显然,如果您想要输入中的百分比而不是数字分数,则可以通过在费率计算中添加/ 100
来实现此目的。