我不确定根据我对问题的描述,我的问题的措词是否正确,因此请编辑它是否更准确。
我试图构建一个印花税计算器来改善我的JS,我有一系列对象使用不同的“带”和“百分比”,这些对象用于根据用户输入来计算税金。我已附上一张图片以更好地理解
我在表格中显示每个波段的税额,并且试图通过在“ TAX”列中查找所有值的总和来查找总税额。
当前,它只是显示最高值。
我已经尽力而为,但没有任何效果,我该如何解决?
这是我的代码,
$(function (jQuery) {
(function stampDutyCalculator() {
var taxbands = [
{
min: 0,
max: 125000,
percent: 0
},
{
min: 125000,
max: 250000,
percent: 0.02
},
{
min: 250000,
max: 925000,
percent: 0.05
},
{
min: 925000,
max: 1500000,
percent: 0.1
},
{
min: 1500000,
max: null,
percent: 0.12
}
];
var secondTaxbands = [
{
min: 0,
max: 125000,
percent: 0.03
},
{
min: 125000,
max: 250000,
percent: 0.05
},
{
min: 250000,
max: 925000,
percent: 0.08
},
{
min: 925000,
max: 1500000,
percent: 0.13
},
{
min: 1500000,
max: null,
percent: 0.15
}
];
var tableRow = "<tr><td>{taxband}</td><td>{percent}</td><td>{taxable}</td><td class='tax'>{TAX}</td></tr>",
table = $("#explained-table"),
results = $("#results"),
effectiveRate = $("#effective-rate");
$('#calculate').on('click', function calculateButton() {
if ($("#input-value").val() !== '') {
calculateStampDuty();
}
});
function calculateStampDuty() {
var bands = taxbands,
userInput = parseInt($("#input-value").val(), 10),
row;
if ($('#second-home').is(':checked')) {
bands = secondTaxbands;
}
if (table.length) {
table.find("tr:gt(0)").remove();
}
var taxableSum = function (x, y) {
var maxBand = (x !== null) ? Math.min(x, userInput) : maxBand = userInput;
return maxBand - y;
},
TAX = function (taxablesum, x) {
return (taxablesum * x).toFixed(2);
},
effectiverate = function(tax) {
return Math.round(tax / userInput * 100).toFixed(1);
},
numberWithCommas = function (x) {
var parts = x.toString().split(".");
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
return parts.join(".");
};
for (var i = 0; i < bands.length; i++) { //for loop to loop through array of objects
var min = bands[i].min, //variables to be used as arguments in functions above, not best practice to declare functions in loop
max = bands[i].max,
pct = bands[i].percent,
taxablesum = taxableSum(max, min),
tax = TAX(taxablesum, pct),
eRate = effectiverate(tax);
if (max !== null) { //replaces template tags with min, max and percent values in object
row = tableRow.replace("{taxband}", "£" + min + " - " + "£" + max).replace("{percent}", (pct * 100) + "%");
} else {
row = tableRow.replace("{taxband}", "£" + min + "+").replace("{percent}", (pct * 100) + "%"); //used for last taxband
}
if (taxablesum < 0) {
row = row.replace("{taxable}", "£" + 0 + ".00").replace("{TAX}", "£" + 0 + ".00");
} else if (userInput > 1500000) {
row = row.replace("{taxable}", "£" + numberWithCommas(taxablesum)).replace("{TAX}", "£" + numberWithCommas(tax));
results.text("£" + numberWithCommas(tax));
effectiveRate.text(eRate + "%");
} else if (userInput > 925000) {
row = row.replace("{taxable}", "£" + numberWithCommas(taxablesum)).replace("{TAX}", "£" + numberWithCommas(tax));
results.text("£" + numberWithCommas(tax));
effectiveRate.text(eRate + "%");
} else if (userInput > 250000) {
row = row.replace("{taxable}", "£" + numberWithCommas(taxablesum)).replace("{TAX}", "£" + numberWithCommas(tax));
results.text("£" + numberWithCommas(tax));
effectiveRate.text(eRate + "%");
} else if (userInput > 125000) {
row = row.replace("{taxable}", "£" + numberWithCommas(taxablesum)).replace("{TAX}", "£" + numberWithCommas(tax));
results.text("£" + numberWithCommas(tax));
effectiveRate.text(eRate + "%");
} else {
row = row.replace("{taxable}", "£" + userInput).replace("{TAX}", "£" + numberWithCommas(tax));
results.text("£" + (numberWithCommas(tax) * 0));
effectiveRate.text(eRate * 0 + "%");
}
table.append(row);
console.log(Number(tax) );
}
}
}());
});
编辑
这是一个小提琴https://jsfiddle.net/p6c1w5r3/
一些按钮功能尚未完成,我想先让计算正确
最佳答案
检查这个小提琴。 https://jsfiddle.net/p6c1w5r3/7/
基本上,我们在循环外创建另一个名为totalTax
的变量。然后在循环中,我们将税值添加到此变量。
最后,您不应每次都设置标签文本,而应在末尾设置一次。
哦,顺便说一句,当金额小于20000时,您的税金计算将返回奇怪的值。