Skip to content

Instantly share code, notes, and snippets.

@raullucero
Last active February 8, 2017 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raullucero/efe1ac14eb6cd749f2439290dc7b6183 to your computer and use it in GitHub Desktop.
Save raullucero/efe1ac14eb6cd749f2439290dc7b6183 to your computer and use it in GitHub Desktop.
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