document.addEventListener('DOMContentLoaded', function() {
// Function to calculate taxes
function calculateTax() {
// Get values from form
let filingStatus = document.getElementById("filingStatus").value;
let income = parseFloat(document.getElementById("income").value) || 0;
let dependents = parseInt(document.getElementById("dependents").value) || 0;
let withholdings = parseFloat(document.getElementById("withholdings").value) || 0;
let stateWithholdings = parseFloat(document.getElementById("stateWithholdings").value) || 0;
let age = parseInt(document.getElementById("age").value) || 0;
let claimedAsDependent = document.getElementById("claimedAsDependent").checked;
// Adjustments for dependents and age
let additionalCredit = (age >= 65) ? 1500 : 0; // Extra credit for seniors
if (claimedAsDependent) {
additionalCredit = 0; // No additional credit if claimed as dependent
}
// Standard deductions for 2024 tax year
let standardDeductions = {
"single": 13850,
"married_joint": 27700,
"married_separate": 13850,
"hoh": 20800
};
let deduction = standardDeductions[filingStatus] || 0;
// Tax brackets (2024) - simplified
let taxBrackets = {
"single": [
[11000, 0.1], [44725, 0.12], [95375, 0.22], [182100, 0.24]
],
"married_joint": [
[22000, 0.1], [89450, 0.12], [190750, 0.22], [364200, 0.24]
],
"married_separate": [
[11000, 0.1], [44725, 0.12], [95375, 0.22], [182100, 0.24]
],
"hoh": [
[15700, 0.1], [59850, 0.12], [95350, 0.22], [182100, 0.24]
]
};
// Apply standard deduction
let taxableIncome = Math.max(income - deduction, 0);
// Calculate tax liability
let taxOwed = 0;
let brackets = taxBrackets[filingStatus] || [];
let previousBracket = 0;
for (let [threshold, rate] of brackets) {
if (taxableIncome > previousBracket) {
let amountTaxed = Math.min(taxableIncome - previousBracket, threshold - previousBracket);
taxOwed += amountTaxed * rate;
previousBracket = threshold;
} else {
break;
}
}
// Apply Child Tax Credit ($2000 per child, simplified, no phaseouts)
let childTaxCredit = Math.min(dependents * 2000, taxOwed);
taxOwed -= childTaxCredit;
// Apply additional credits (e.g., senior credit)
taxOwed = Math.max(taxOwed - additionalCredit, 0);
// Calculate final refund or amount owed
let refund = withholdings + stateWithholdings - taxOwed;
let resultText = refund >= 0 ? `Estimated Refund: $${refund.toFixed(2)}` : `Amount Owed: $${Math.abs(refund).toFixed(2)}`;
document.getElementById("result").innerText = resultText;
}
// Render the form
let container = document.getElementById('taxEstimatorContainer');
container.innerHTML = `
Tax Return Estimator
`;
// Attach event listener to button
document.querySelector("button").addEventListener("click", calculateTax);
});