2 Replies Latest reply on Feb 19, 2014 7:00 PM by alvinmac

# Javascript for Tiered Income Tax Calculation

Hi everyone,

I'm trying to write a custom calculation script for the tiered income tax computation:

1. Income up to \$40,000 (20%)

2. Income between \$40,001 and \$60,000 (30%)

3. Income between \$60,001 and \$100,000(35%)

4. Income over \$100,001 (40%)

If the income is \$80,000, tax computation would be:

Total Tax = (40,000 * 20%) +  (20,000 * 30%) + (20,000 * 35%)

I have managed to get the correct calculation for #1 but I'm having a hard time for the rest. Here's my script for item #1

var nValue = Number(this.getField("Income").value);

if(nValue<40001) { event.value = math.max(0, nValue*0.20);

}

else {event.value = 40000*0.20;

}

Also, when I tried to change the amount in Income, the taxes didn't change.

• ###### 1. Re: Javascript for Tiered Income Tax Calculation

I think you need a script like:

event.value = 0;
var nValue = this.getField("Income").value;

if(nValue > 0 && nValue < 40000) {
// 20% of income;
event.value = nValue * .2;
}
if(nValue >= 40000 && nValue < 60000) {
// 30% of income over 40,000 + 20% of 40000;
event.value = (.3 * (nValue - 40000)) + (.2 * 40000)  ;
}
if(nValue >= 60000 && nValue < 100000) {
// 35% amount over 60,000 + 30% of 20,000 + 20% of 40,000
event.value = (.35 *(nValue - 60000) + (.3 * 20000) +  (.2 * 40000 );
}
if(nValue >= 100000) {
// 40% amount over 100,000 + 35% of 40,000 + 30% of 20000 + 20% of 40,000
event.value = (.4 * (nValue - 100000)) + (.35 * 40000) + (.3 * 20000)  + (.2 * 40000) ;
}

For your code, did you check the JavaScript console?

Do understand how interpreted languages handle errors?

• ###### 2. Re: Javascript for Tiered Income Tax Calculation

Thank you so much!!! You've been very helpful