JS calculations - Can someone tell me what I've done wrong?

goldy300

New Member
Messages
33
Reaction score
0
Points
0
Not sure what I've done wrong here.

Here's whats required:
1.[FONT=&quot] [/FONT]TAX (selection structure)

Create a web application which will determine your tax for the 2006-2007 financial year.
The tax rates and medicare levy are shown below.
Allow the user to enter their WEEKLY salary and display their annual salary, annual tax, medicare levy and total tax amounts. Ensure all currency amounts are displayed in the correct format ($ sign with two decimal places).
Save your program as tax.htm
RANGE($)
RATE(%)
0-6,000​
0​
6,001-25,000​
15​
25,001-75,000​
30​
75,001-150,000​
40​
More than 150,000​
45​
Medicare levy
1.5​

Here's what I've done.

HTML:
<script language="javascript">
function calc_tax(){

var tax;
var income = parseFloat(document.taxform.income.value);

var sal = income * 52;
var taxpaid = sal * tax;
var medicare = sal * 0.015;


if(sal <= 6,000){
    tax = 0;
    }
else if(sal >= 6001 && sal <= 25000){
    tax = 0.15;
    }
else if(sal >= 25001 && sal <= 75000){
    tax = 0.30;
    }
else if(sal >= 75001 && sal <= 150000){
    tax = 0.40;
    }
else if(sal >= 150000){
    tax = 0.45;
    }


document.getElementById("tax").innerHTML = "<center><h3>Tax Information</h3></center>";
document.getElementById("tax").innerHTML = "Your annual salary is $" + sal.toFixed(2) + "<p>";
document.getElementById("tax").innerHTML = "The amount of tax you pay is $" + taxpaid.toFixed(2) + "<p>";
document.getElementById("tax").innerHTML = "Your medicare levy is $" + medicare.toFixed(2) + "<p>";
document.getElementById("tax").innerHTML = "Your total payments are $" + (taxpaid + medicare).toFixed(2) + "<p>";

}
</script>
 

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
Code:
<script language="javascript">
function calc_tax(){

var tax;
var income = parseFloat(document.taxform.income.value);


var sal = income * 52;

if(sal <= 6,000){
    tax = 0;
}
else if(sal >= 6001 && sal <= 25000){
    tax = 0.15;
}
else if(sal >= 25001 && sal <= 75000){
    tax = 0.30;
}
else if(sal >= 75001 && sal <= 150000){
    tax = 0.40;
}
else if(sal >= 150000){
    tax = 0.45;
}

var taxpaid = sal * tax;
var medicare = sal * 0.015;


document.getElementById("tax").innerHTML = "<center><h3>Tax Information</h3></center>";
document.getElementById("tax").innerHTML = "Your annual salary is $" + sal.toFixed(2) + "<p>";
document.getElementById("tax").innerHTML = "The amount of tax you pay is $" + taxpaid.toFixed(2) + "<p>";
document.getElementById("tax").innerHTML = "Your medicare levy is $" + medicare.toFixed(2) + "<p>";
document.getElementById("tax").innerHTML = "Your total payments are $" + (taxpaid + medicare).toFixed(2) + "<p>";

}
</script>
 

natsuki

New Member
Messages
112
Reaction score
0
Points
0
there's also another trick to it. instead of check for if ((sal >= min) && (sal <= max)), you can do the reverse, start from the biggest amount and check if sal > min, if not, then do the next check in decreasing order:

PHP:
if (sal > 150000)
// tax = 45%
else if (sal > 75000) // you only get here if sal is <= 150000 so simple checking > 75000 would suffice
// 40%
else if (sal > 25000)
// 30%
else if (sal > 6000)
// 15%
else if (sal >= 0)
// 0%
This trick eliminates the need for checking 2 expressions and &&ing them, it's faster and actually even safer.
 
Last edited:
Top