+ Reply to Thread
Results 1 to 3 of 3

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

  1. #1
    goldy300's Avatar
    goldy300 is offline x10Hosting Member goldy300 is an unknown quantity at this point
    Join Date
    Jul 2008
    Location
    Australia
    Posts
    33

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

    Not sure what I've done wrong here.

    Here's whats required:
    1.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 Code:
    <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>

  2. #2
    xPlozion's Avatar
    xPlozion is offline x10 Elder xPlozion is an unknown quantity at this point
    Join Date
    Mar 2008
    Location
    Delaware, USA
    Posts
    872

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

    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>

  3. #3
    natsuki's Avatar
    natsuki is offline x10 Sophmore natsuki is an unknown quantity at this point
    Join Date
    Sep 2008
    Posts
    112

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

    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 Code:
    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 by natsuki; 11-30-2008 at 04:52 PM.

+ Reply to Thread

Similar Threads

  1. What is wrong?
    By VeggieBoy in forum Off Topic
    Replies: 10
    Last Post: 03-08-2008, 05:44 AM
  2. Wrong Disk Usage Reading
    By 818Papi in forum Free Hosting
    Replies: 4
    Last Post: 01-29-2008, 09:06 PM
  3. phpmyadmin wrong username and password?
    By shaunNO2007 in forum Free Hosting
    Replies: 0
    Last Post: 01-17-2008, 12:24 PM
  4. SOme thing wrong with addon domain
    By dharmil in forum Free Hosting
    Replies: 2
    Last Post: 08-24-2006, 12:13 PM
  5. Help please, Server down or something wrong?
    By flyingwolf in forum Free Hosting
    Replies: 15
    Last Post: 05-19-2005, 12:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers