Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
const tax = [[0, 0.1],
[1000, 0.2],
[4000, 0.3],
[6000, 0.4],
[6700, 0.6]];
const calcTax = (taxesTable, income) => {
let total = 0;
let index = taxesTable.findIndex((item) => {
return item[0] > income;
});
if(index === -1) {
index = taxesTable.length;
}
console.log(index);
for(let i = 0; i < index; i++) {
if(index === 1) {
total = (income * taxesTable[i][1]);
console.log(total)
} else if(i + 1 === index){
console.log((income - taxesTable[i][0]) * taxesTable[i][1])
total += (income - taxesTable[i][0]) * taxesTable[i][1];
} else {
console.log((taxesTable[i + 1][0] - taxesTable[i][0]) * taxesTable[i][1]);
total += (taxesTable[i + 1][0] - taxesTable[i][0]) * taxesTable[i][1];
}
}
console.log(total);
console.log('-'.repeat(100));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment