How to Reverse-Engineer a Calculator's Logic in JavaScript?

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:

  1. 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.
  2. 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!

It seems like you should look up the tax brackets and rules from an official source

Italian tax rate goes by income bracket, in this way (the numbers are made up, but the logic is that):
if the income up to 15000 is taxed at 22% and the income in the range 15000.01-25000 is taxed at 25%
if your income is 20000, then the slice of 15000 is taxed at 22% and then the 5000 that are int he bracket above is taxed at 25%

the numbers at which the income changes is not a round number, but I guess you could be able to solve this with some math, or just find the current tax brackets and rates

Thank you both, this is very helpful. @ILM I understand the logic of taxing income in different “slices” now. That clarifies the core problem in my code.
@pkdvalis, following your advice, I searched for examples of Italian salary calculators. This was one of the results: https://calcolostipendionettoonline.it. It seems to demonstrate the progressive logic we’ve been discussing. For example, the change in net salary between a gross of €28,000 and €35,000 isn’t linear, which appears to match the “income slice” method. I will use it as a reference to test my code against as I implement the logic you’ve both explained.Thanks again for the guidance.

I would suggest you search official Agenzia delle Entrate tax brackets and tax rate documentation if you want a precise thing

1 Like