Results 1 to 3 of 3

Thread: Javascript Calculation help!

  1. #1
    Join Date
    Apr 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Javascript Calculation help!

    All,

    Below find my JS which is supposed to do the following:
    Take user supplied numerical values from 7 fields, perform the calculation function that's in the code and place it into the Total box / field.

    Needless to say ...or I wouldn't be here...it doesn't work.
    I thought maybe the problem was in the function, but I'm not sure

    Thanks to all who can help!

    NewbieScott

    ================================

    <html>
    <head>
    <title>Test Prototype</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <style type="text/css">
    * {
    margin:0;
    padding:0;
    }
    body {
    background:#fff;
    }
    p {
    margin:2em;
    }
    </style>
    <script type="text/javascript">

    function calc(D1,D2,D3,D4,D5,D6,D7){

    return (((D1*2.036+D3)/(273+D5))-((D2*2.036+D4)/(273+D6)))*238*(3050/D7);


    var D1=parseInt(D1.value);
    var D2=parseInt(D2.value);
    var D3=parseInt(D3.value);
    var D4=parseInt(D4.value);
    var D5=parseInt(D5.value);
    var D6=parseInt(D6.value);
    var D7=parseInt(D7.value);
    if (isNaN(D1)) {D1=0;}
    if (isNaN(D2)) {D2=0;}
    if (isNaN(D3)) {D3=0;}
    if (isNaN(D4)) {D4=0;}
    if (isNaN(D5)) {D5=0;}
    if (isNaN(D6)) {D6=0;}
    if (isNaN(D7)) {D7=0;}
    document.getElementById('total').value = D1+D2+D3+D4+D5+D6+D7;
    }

    window.onload=function() {
    var D1=document.getElementById('D1');
    var D2=document.getElementById('D2');
    var D3=document.getElementById('D3');
    var D4=document.getElementById('D4');
    var D5=document.getElementById('D5');
    var D6=document.getElementById('D6');
    var D7=document.getElementById('D7');
    D1.onkeyup=function() {calc(D1,D2,D3,D4,D4,D6,D7);
    D2.onkeyup=function() {calc(D1,D2,D3,D4,D4,D6,D7);
    D3.onkeyup=function() {calc(D1,D2,D3,D4,D4,D6,D7);
    D4.onkeyup=function() {calc(D1,D2,D3,D4,D4,D6,D7);
    D5.onkeyup=function() {calc(D1,D2,D3,D4,D4,D6,D7);
    D6.onkeyup=function() {calc(D1,D2,D3,D4,D4,D6,D7);
    D7.onkeyup=function() {calc(D1,D2,D3,D4,D5,D6,D7);
    }
    </script>
    </head>
    <body>
    <form action="#">
    <p><label> Number 1: <input id="D1"></label>
    <p><label> Number 2: <input id="D2"></label>
    <p><label> Number 3: <input id="D3"></label>
    <p><label> Number 4: <input id="D4"></label>
    <p><label> Number 5: <input id="D5"></label>
    <p><label> Number 6: <input id="D6"></label>
    <p><label> Number 7: <input id="D7"></label>


    <p>Total = <input id="total"></p>
    </p>
    </body>
    </html>

  2. #2
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    Code:
    <html>
    <head>
    <title>Test Prototype</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <style type="text/css">
    * {
    margin:0;
    padding:0;
    }
    body {
    background:#fff;
    }
    p {
    margin:2em;
    }
    </style>
    <script type="text/javascript">
    
    function calc(D1,D2,D3,D4,D5,D6,D7){
    
    //return (((D1*2.036+D3)/(273+D5))-((D2*2.036+D4)/(273+D6)))*238*(3050/D7);
    
    
     D1=parseInt(D1.value);
     D2=parseInt(D2.value);
     D3=parseInt(D3.value);
     D4=parseInt(D4.value);
     D5=parseInt(D5.value);
     D6=parseInt(D6.value);
     D7=parseInt(D7.value);
     if (isNaN(D1)) {
      D1=0;
     }
     if (isNaN(D2)) {
      D2=0;
     }
     if (isNaN(D3)) {
      D3=0;
     }
     if (isNaN(D4)) {
      D4=0;
     }
     if (isNaN(D5)) {
      D5=0;
     }
     if (isNaN(D6)) {
      D6=0;
     }
     if (isNaN(D7)) {
      D7=0;
     }
     document.getElementById('total').value = D1+D2+D3+D4+D5+D6+D7;
    }
    
    window.onload=function() {
     var D1=document.getElementById('D1');
     var D2=document.getElementById('D2');
     var D3=document.getElementById('D3');
     var D4=document.getElementById('D4');
     var D5=document.getElementById('D5');
     var D6=document.getElementById('D6');
     var D7=document.getElementById('D7');
     D1.onkeyup=function() {
      calc(D1,D2,D3,D4,D5,D6,D7);
     }
     D2.onkeyup=function() {
      calc(D1,D2,D3,D4,D5,D6,D7);
     }
     D3.onkeyup=function() {
      calc(D1,D2,D3,D4,D5,D6,D7);
     }
     D4.onkeyup=function() {
      calc(D1,D2,D3,D4,D5,D6,D7);
     }
     D5.onkeyup=function() {
      calc(D1,D2,D3,D4,D5,D6,D7);
     }
     D6.onkeyup=function() {
      calc(D1,D2,D3,D4,D5,D6,D7);
     }
     D7.onkeyup=function() {
      calc(D1,D2,D3,D4,D5,D6,D7);
     }
    }
    </script>
    </head>
    <body>
    <form action="#">
    <p><label> Number 1: <input id="D1"></label>
    <p><label> Number 2: <input id="D2"></label>
    <p><label> Number 3: <input id="D3"></label>
    <p><label> Number 4: <input id="D4"></label>
    <p><label> Number 5: <input id="D5"></label>
    <p><label> Number 6: <input id="D6"></label>
    <p><label> Number 7: <input id="D7"></label>
    
    
    <p>Total = <input id="total"></p>
    </p>
    </form>
    </body>
    </html>
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  3. #3
    Join Date
    Jan 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Javascript Calculation Help

    Hey everyone, IM STUCK..

    I have this formula that I need to turn into a javascript formula.

    If (A > 25000 {
    A - 25000 = B
    Int(B / 50000) = C
    If (C < 1) {
    B / 5000 = E
    E * 30 = F
    If (F > 149) {
    rec3Total = 149
    }
    } else{
    (B - (C * 50000)) / 5000 = E
    (C * 149) + (E * 30) = F
    }
    } else {
    F = 0
    }

    This is what the formula currently is:

    //Calculate Records Total
    if ($("input[name^=add3Records]").val() > 25000) {
    $("[id^=rec3Total]").val("0")
    }

    else {
    $("[id^=rec3Total]").calc(
    // the equation to use for the calculation
    "Math.ceil((add3Records - 25000) / 5000) * 30",

    // define the variables used in the equation, these can be a jQuery object
    {
    add3Records: $("input[name^=add3Records]")
    });
    }


    So the new formula the A stands for: input[name^=add3Records]..


    Can anyone please give me some insight on this.

    Thanks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •