Finding correct tax bracket with iteration

Hello!

taxBracket is finding the tax payed for each step of the tax bracket (stepOne, stepTwo, etc ...)

What I want to do is find if myIncome is:
--- higher than low and equal to high, if so, return the full sum
--- higher than low, but lower than high, if so subtract low from myIncome and multiply with tax to find a new sum
--- lower than low, ignore the bracket

I'v tried all day with for loops, if...else statements, but I don't understand what to use to calculate all of those three conditions ...

const myIncome = 650000;

const taxBracket = (low, high, tax) => {
    return {
        low,
        high,
        tax,
        sum() {
            return (this.high - this.low) * this.tax;
        }
    }

};

const stepOne = taxBracket(198350, 279150, 0.017);
const stepTwo = taxBracket(279150, 642590, 0.04);
const stepThree = taxBracket(642590, 926800, 0.134);
const stepFour = taxBracket(926800, 1500000, 0.164);
const stepFive = taxBracket(1500000, Infinity, 0.174);
Was this page helpful?