Can someone help me with this javascript?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aubderk
    New Member
    • Dec 2011
    • 1

    #1

    Can someone help me with this javascript?

    I am working on a project and I am struggling with the javascript. I am creating a form that should calculate a total amount based on the quantity a user chooses. The calcTotal function should calculate the tax amount and overall total. The calcShip function should calculate the shipping amount based upon the subtotal. I'm thinking there are probably multiple errors along the way and any help is greatly appreciated.

    I am getting the "missing ; before statement" error when I run the script in the browser.

    HTML
    Code:
    <head>
    
    
       <title>Aubrey's Flowers</title>
       <link href="aderkstyles.css" rel="stylesheet" type="text/css" />
       <script src="aderkjs.js" type="text/javascript"></script>
    
    
    </head>
    
    <body>
    <div id="main">
    <a href="aderkfinal.htm"><img src="logo.png"></a>
    <table width="400" border="0" cellspacing="25">
      <tr>
        <td><a href="aderkfinal-peonies.htm"><img src="peoniesthumb.jpg"></a></td>
        <td><a href="aderkfinal-lavendar.htm"><img src="lavendarthumb.jpg"></a></td>
        <td><a href="aderkfinal-babiesbreath.htm"><img src="babiesbreaththumb.jpg"></a></td>
        <td><a href="aderkfinal-succulents.htm"><img src="succulentsthumb.jpg"></a></td>
      </tr>
      <tr>
        <td valign="top"><strong>Peonies</strong><br>
            Large, variously colored flowers with numerous stamens and several pistils.<br>
            <a href="aderkfinal-peonies.htm"><strong>BUY NOW</strong></a></td>
        <td valign="top"><strong>Lavendar</strong><br>
            Clusters of small purplish flowers. Yield an aromatic oil used in perfumery.<br>
            <a href="aderkfinal-lavendar.htm"><strong>BUY NOW</strong></a></td>
        <td valign="top"><strong>Baby's Breath</strong><br>
            Small, white flowers in profusely branched panicles. Especially popular in bouquets and arrangements.<br>
            <a href="aderkfinal-babiesbreath.htm"><strong>BUY NOW</strong></a></td>
        <td valign="top"><strong>Succulents</strong><br>
            Thick, fleshy, water-storing leaves or stems.<br>
            <a href="aderkfinal-succulents.htm"><strong>BUY NOW</strong></a></td>
      </tr>
    
    </table>
    </div>
    </body>
    
    </html>
    javascript
    Code:
    /*
    
    
       Functions List:
       
       calcTotal()
          Calculates total cost amount
    
       calcShip()
          Calculates shipping amount
    
    
    */
    
    
    calcTotal(){
    
    var price = document.forms[0].unitprice.value;
    var quantity = document.forms[0].qty.value;
    
    document.forms[0].sbtl.value = price * quantity;
    
    var subtotal = document.forms[0].sbtl.value;
    
    if (document.forms[0].state.value = "PA"){
    document.forms[0].tax.value="6";
    var pataxamount = subtotal * .06;
    var shippingamount = calcShip();
    pataxamount + shippingamount = document.forms[0].total.value;
    }
    
    else if (document.forms[0].state.value = "NJ"){
    document.forms[0].tax.value="7";
    var njtaxamount = subtotal * .07;
    var shippingamount = calcShip();
    njtaxamount + shippingamount = document.forms[0].total.value;
    }
    
    else if (document.forms[0].state.value = "NY"){
    document.forms[0].tax.value="8";
    var nytaxamount = subtotal * .08;
    var shippingamount = calcShip();
    nytaxamount + shippingamount = document.forms[0].total.value;
    }
    
    else {
    document.forms[0].tax.value="0";
    var shippingamount = calcShip();
    subtotal + shippingamount = document.forms[0].total.value;
    }
    }
    
    calcShip(){
    
    if (subtotal <= 25){
    document.forms[0].shipping.value="2.50";
    }
    
    if (subtotal > 25 && subtotal <= 75){
    document.forms[0].shipping.value="5.00";
    }
    
    if (subtotal > 75 && subtotal <= 150){
    document.forms[0].shipping.value="7.50";
    }
    
    else {
    document.forms[0].shipping.value="10.00"
    }
    }
    Thanks!
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    In your calc total function, your addition and assignment needs to be flipped.
    a = b + c
    not
    b + c = a

    Comment

    Working...