Skip to content

Instantly share code, notes, and snippets.

@panphora
Created December 2, 2023 00:27
Show Gist options
  • Save panphora/42ad803ca2d4242d6bdc7668616a057d to your computer and use it in GitHub Desktop.
Save panphora/42ad803ca2d4242d6bdc7668616a057d to your computer and use it in GitHub Desktop.
/*
# taxes.js - calculate Federal Taxes
## Usage:
taxes.bracket(0, 22000, 0.10)
taxes.bracket(22001, 89450, 0.12);
taxes.bracket(89451, 190750, 0.22);
taxes.income(100000);
console.log("Total Taxes: ", taxes.calculate());
console.log("Taxes Per Bracket: ", taxes.calculatePerBraket());
*/
const taxes = {
brackets: [],
income: 0,
// Method to add a tax bracket
bracket: function(min, max, rate) {
this.brackets.push({ min, max, rate });
},
// Method to set the income
income: function(amount) {
this.income = amount;
},
// Method to calculate total expected taxes
calculate: function() {
let totalTax = 0;
for (const bracket of this.brackets) {
if (this.income > bracket.min) {
const taxableIncome = Math.min(this.income, bracket.max) - bracket.min;
totalTax += taxableIncome * bracket.rate;
}
}
return totalTax;
},
// Method to calculate taxes per bracket
calculatePerBraket: function() {
let taxesPerBracket = [];
for (const bracket of this.brackets) {
if (this.income > bracket.min) {
const taxableIncome = Math.min(this.income, bracket.max) - bracket.min;
const tax = taxableIncome * bracket.rate;
taxesPerBracket.push({
range: `${bracket.min}-${bracket.max}`,
rate: bracket.rate,
tax: tax
});
}
}
return taxesPerBracket;
}
};
// Usage
taxes.bracket(0, 22000, 0.10)
taxes.bracket(22001, 89450, 0.12);
taxes.bracket(89451, 190750, 0.22);
taxes.income(100000);
console.log("Total Taxes: ", taxes.calculate());
console.log("Taxes Per Bracket: ", taxes.calculatePerBraket());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment