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);
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);
5 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Å Marlon G
Å Marlon GOP2y ago
Yes! I totally understand that it's confusing ... 😅 Could I ask what is confusing, because I worry I will just write the same thing one more time, but slightly differently. Is it my goal that is confusing? This is a try to explain it in code ...
[stepOne, stepTwo, stepThree, stepFour, stepFive].forEach((elm) => {
console.log(elm);
if (myIncome > elm.low && myIncome === elm.high) {
return elm.sum();
}
if (myIncome > elm.low && myIncome < elm.high) {
return (myIncome - elm.low) * elm.tax;
}
if (myIncome < elm.low) {
return 'Income is to low for bracket';
}
});
[stepOne, stepTwo, stepThree, stepFour, stepFive].forEach((elm) => {
console.log(elm);
if (myIncome > elm.low && myIncome === elm.high) {
return elm.sum();
}
if (myIncome > elm.low && myIncome < elm.high) {
return (myIncome - elm.low) * elm.tax;
}
if (myIncome < elm.low) {
return 'Income is to low for bracket';
}
});
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Å Marlon G
Å Marlon GOP2y ago
Yes! As far as I understand it, the if... statements are doing what I want (love the first if, I obviously only need to check if it's the same ... 👍 ) So I added myIncome at the top, and it works, but it doesn't print anything even if there is a console.log in there. This is exzctly what I'm running:
const myIncome = 650000;

const steps = {
stepOne: taxBracket(198350, 279150, 0.017),
stepTwo: taxBracket(279150, 642590, 0.04),
stepThree: taxBracket(642590, 926800, 0.134),
stepFour: taxBracket(926800, 1500000, 0.164),
stepFive: taxBracket(1500000, Infinity, 0.174),
};

for (key in steps) {
const elm = steps[key];
console.log(
key +
": " +
(() => {
if (myIncome === elm.high) {
return elm.sum();
} else if (myIncome > elm.low && myIncome < elm.high) {
return (myIncome - elm.low) * elm.tax;
} else if (myIncome < elm.low) {
return "Income is to low for bracket";
}
})()
);
}
const myIncome = 650000;

const steps = {
stepOne: taxBracket(198350, 279150, 0.017),
stepTwo: taxBracket(279150, 642590, 0.04),
stepThree: taxBracket(642590, 926800, 0.134),
stepFour: taxBracket(926800, 1500000, 0.164),
stepFive: taxBracket(1500000, Infinity, 0.174),
};

for (key in steps) {
const elm = steps[key];
console.log(
key +
": " +
(() => {
if (myIncome === elm.high) {
return elm.sum();
} else if (myIncome > elm.low && myIncome < elm.high) {
return (myIncome - elm.low) * elm.tax;
} else if (myIncome < elm.low) {
return "Income is to low for bracket";
}
})()
);
}
I have to honest – I'm a newbie so even though I've gone through all of the stuff you put into this code, it is still a bit overwhelming. I need to sit down to really understand it. Oh! I understood one mistake I did ... this is the exact code I'm running:
const myIncome = 650000;


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

};

const steps = {
stepOne: taxBracket(198350, 279150, 0.017),
stepTwo: taxBracket(279150, 642590, 0.04),
stepThree: taxBracket(642590, 926800, 0.134),
stepFour: taxBracket(926800, 1500000, 0.164),
stepFive: taxBracket(1500000, Infinity, 0.174),
};

for (key in steps) {
const elm = steps[key];
console.log(
key +
": " +
(() => {
if (myIncome === elm.high) {
return elm.sum();
} else if (myIncome > elm.low && myIncome < elm.high) {
return (myIncome - elm.low) * elm.tax;
} else if (myIncome < elm.low) {
return "Income is to low for bracket";
}
})()
);
}
const myIncome = 650000;


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

};

const steps = {
stepOne: taxBracket(198350, 279150, 0.017),
stepTwo: taxBracket(279150, 642590, 0.04),
stepThree: taxBracket(642590, 926800, 0.134),
stepFour: taxBracket(926800, 1500000, 0.164),
stepFive: taxBracket(1500000, Infinity, 0.174),
};

for (key in steps) {
const elm = steps[key];
console.log(
key +
": " +
(() => {
if (myIncome === elm.high) {
return elm.sum();
} else if (myIncome > elm.low && myIncome < elm.high) {
return (myIncome - elm.low) * elm.tax;
} else if (myIncome < elm.low) {
return "Income is to low for bracket";
}
})()
);
}
It returns this: stepOne: undefined stepTwo: undefined stepThree: 992.94 stepFour: Income is to low for bracket stepFive: Income is to low for bracket stepOne and stepTwo should also have a number, specifically the sum of this function for each step:
sum() {
return (this.high - this.low) * this.tax;
sum() {
return (this.high - this.low) * this.tax;
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server