I'm in the process of teaching myself javascript and it looks at though I've hit a road block. As an exercise I am trying to build a tax calculator.
Here is an object I built that contains the tax brackets:
var taxBracket = [
{bracket: 1, from:0, to:18200, percentage :0, amount:0},
{bracket: 2, from:18201, to:37000, percentage :19, over:18200, amount:0},
{bracket: 3, from:37001, to:80000, percentage :32.5, over:37000, amount:3752},
{bracket: 4, from:80001, to:180000, percentage :37, over:80000, amount:17547},
{bracket: 5, from:180001, to:0, percentage :45, over:180000, amount:54547}];
and here is the function that loops through object to find the corresponding tax bracket of y
function returnTax (y){
for(var x in taxBracket){
if (y >= taxBracket[x].from && y <= taxBracket[x].to){
var z = taxBracket[x].amount + ((grossIncome-taxBracket[x].over) * (taxBracket[x].percentage/100));
return z;
}
};
The issue is that if y
is over 180000, it errors out as to
is 0. Is this only solution to this an else statement repeating the functions of the if statement? Thank for your help!