Hey everyone,
I’m trying to improve my JavaScript skills by recreating a tool I found online. It’s an Italian net salary calculator, and I’m trying to build my own version of it to understand the complex tax logic.
My challenge is that I’m trying to figure out the calculation rules just by inputting different numbers into the original calculator and seeing the results. I can see the final net salary, but I’m struggling to write the JavaScript code that produces the same output.
Here are the specific things I’m trying to figure out:
- Progressive Tax Brackets: I can tell there are different tax rates at different income levels, but I can’t figure out the exact formula. For example, a gross salary of €28,000 gives one result, but at €35,000, the tax rate seems to change in a non-linear way.
- Order of Deductions: I’m not sure what gets subtracted first. Is it social security, then tax? Or the other way around? The order changes the final result.
Here is my best guess so far in code. It’s not giving the same results as the online tool I’m using as a reference.
JavaScript
// My attempt to match the calculator's output
function calculateNetSalary(grossSalary) {
const socialContributionsRate = 0.0919; // This is a guess
let taxableIncome = grossSalary - (grossSalary * socialContributionsRate);
let irpefTax = 0;
if (taxableIncome <= 28000) {
irpefTax = taxableIncome * 0.23;
} else {
irpefTax = (28000 * 0.23) + ((taxableIncome - 28000) * 0.35);
}
const netSalary = grossSalary - (grossSalary * socialContributionsRate) - irpefTax;
return netSalary.toFixed(2);
}
// When I test it, my numbers don't match the online calculator.
const gross = 35000;
console.log(`My function's result: €${calculateNetSalary(gross)}`);
// The result from my function is different from the live calculator.
My question is, has anyone tried to do something like this before? How do you figure out the logic of a tool when you can only see the inputs and outputs?
Maybe my irpefTax logic is completely wrong. Any advice on how to code progressive tax brackets, or tips on “reverse-engineering” this kind of logic, would be amazing.
Thanks!