Last active
December 2, 2023 00:45
-
-
Save panphora/1f1698c5e6501bcbfa795c44cba8501b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
# calculate your taxes in your browser console | |
## Usage ## | |
taxes.bracket(0, 22000, .10) | |
taxes.bracket(22001, 89450, .12); | |
taxes.bracket(89451, 190750, .22); | |
taxes.state(.05); | |
taxes.income(100000); | |
console.log("Total Taxes: ", taxes.calculate()); | |
console.log("Federal Taxes: ", taxes.calculateFederal()); | |
console.log("State Taxes: ", taxes.calculateState()); | |
console.log("Taxes Per Bracket: ", taxes.calculatePerBracket()); | |
*/ | |
// Define the taxes object | |
const taxes = { | |
brackets: [], | |
income: 0, | |
stateTaxRate: 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 set the state tax rate | |
state: function(taxRate) { | |
this.stateTaxRate = taxRate; | |
}, | |
// Method to calculate state tax | |
calculateState: function() { | |
return this.income * this.stateTaxRate; | |
}, | |
// Method to calculate federal tax | |
calculateFederal: function() { | |
let federalTax = 0; | |
for (const bracket of this.brackets) { | |
if (this.income > bracket.min) { | |
const taxableIncome = Math.min(this.income, bracket.max) - bracket.min; | |
federalTax += taxableIncome * bracket.rate; | |
} | |
} | |
return federalTax; | |
}, | |
// Method to calculate total expected taxes | |
calculate: function() { | |
const federalTax = this.calculateFederal(); | |
const stateTax = this.calculateState(); | |
return federalTax + stateTax; | |
}, | |
// Method to calculate taxes per bracket including state tax | |
calculatePerBracket: 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 | |
}); | |
} | |
} | |
// Add state tax as a separate bracket | |
taxesPerBracket.push({ | |
range: `State Tax`, | |
rate: this.stateTaxRate, | |
tax: this.calculateState() | |
}); | |
return taxesPerBracket; | |
} | |
}; | |
// Usage | |
taxes.bracket(0, 22000, .10) | |
taxes.bracket(22001, 89450, .12); | |
taxes.bracket(89451, 190750, .22); | |
taxes.state(.05); | |
taxes.income(100000); | |
console.log("Total Taxes: ", taxes.calculate()); | |
console.log("Federal Taxes: ", taxes.calculateFederal()); | |
console.log("State Taxes: ", taxes.calculateState()); | |
console.log("Taxes Per Bracket: ", taxes.calculatePerBracket()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment