Help with loop

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Targa

    Help with loop

    Im using the script below to total lines on an invoice.
    There are 3 fields per line: Line_Qty(n), Line_Unit_Price (n) and
    Line_Item_Subto tal(n).
    It works when there is a fixed number of lines but I need to have it work
    for any amount of lines.

    Can somebody help me convert this into a loop?

    Thanks in advance!

    function calc(form) {
    var sum = 0;
    var rowsum;
    var quantity = 1

    // Add Lines
    if ( parseFloat(form .Line_Qty1.valu e) &&
    parseFloat(form .Line_Unit_Pric e1.value) ) {
    quantity += parseInt(form.L ine_Qty1.value) ;
    form.Line_Qty1. value = parseInt(form.L ine_Qty1.value) ;
    form.Line_Unit_ Price1.value = parseFloat(form .Line_Unit_Pric e1.value);
    rowsum = form.Line_Qty1. value * form.Line_Unit_ Price1.value;
    sum += rowsum;
    form.Line_Unit_ Price1.value = money(form.Line _Unit_Price1.va lue);
    form.Line_Item_ Subtotal1.value = money(rowsum)
    }
    if ( parseFloat(form .Line_Qty2.valu e) &&
    parseFloat(form .Line_Unit_Pric e2.value) ) {
    quantity += parseInt(form.L ine_Qty2.value) ;
    form.Line_Qty2. value = parseInt(form.L ine_Qty2.value) ;
    form.Line_Unit_ Price2.value = parseFloat(form .Line_Unit_Pric e2.value);
    rowsum = form.Line_Qty2. value * form.Line_Unit_ Price2.value;
    sum += rowsum;
    form.Line_Unit_ Price2.value = money(form.Line _Unit_Price2.va lue);
    form.Line_Item_ Subtotal2.value = money(rowsum)
    }
    if ( parseFloat(form .Line_Qty3.valu e) &&
    parseFloat(form .Line_Unit_Pric e3.value) ) {
    quantity += parseInt(form.L ine_Qty3.value) ;
    form.Line_Qty3. value = parseInt(form.L ine_Qty3.value) ;
    form.Line_Unit_ Price3.value = parseFloat(form .Line_Unit_Pric e3.value);
    rowsum = form.Line_Qty3. value * form.Line_Unit_ Price3.value;
    sum += rowsum;
    form.Line_Unit_ Price3.value = money(form.Line _Unit_Price3.va lue);
    form.Line_Item_ Subtotal3.value = money(rowsum)
    }
    and so on......



  • Shawn Milo

    #2
    Re: Help with loop

    "Targa" <targa1_removet historeply_@all tel.net> wrote in message news:<f9yvc.322 $783.131@fe39.u senetserver.com >...[color=blue]
    > Im using the script below to total lines on an invoice.
    > There are 3 fields per line: Line_Qty(n), Line_Unit_Price (n) and
    > Line_Item_Subto tal(n).
    > It works when there is a fixed number of lines but I need to have it work
    > for any amount of lines.
    >
    > Can somebody help me convert this into a loop?[/color]
    <snip>


    Try something like this (untested).
    Some people would say that using
    eval() is a bad thing, so maybe
    one of them will take the time to post an
    alternative.

    Shawn


    // -- code follows -- //

    var lineNum = 1;

    var qty;
    var prc;
    var subt;

    qty = eval('form.Line _Qty' + lineNum);
    prc = eval('form.Line _Unit_Price' + lineNum);
    subt = eval('form.Line _Subtotal' + lineNum);

    while (qty.value){

    if ( parseFloat(qty. value) && parseFloat(prc. value) ) {
    quantity += parseInt(qty.va lue);
    qty.value = parseInt(qty.va lue);
    prc.value = parseFloat(prc. value);
    rowsum = qty.value * prc.value;
    sum += rowsum;
    prc.value = money(prc.value );
    subt.value = money(rowsum)
    }


    lineNum++;
    qty = eval('form.Line _Qty' + lineNum);
    prc = eval('form.Line _Unit_Price' + lineNum);
    subt = eval('form.Line _Subtotal' + lineNum);

    }

    // -- end of code -- //

    Comment

    • Richard Cornford

      #3
      Re: Help with loop

      Shawn Milo wrote:
      <snip>[color=blue]
      > Try something like this (untested).
      > Some people would say that using
      > eval() is a bad thing, so maybe
      > one of them will take the time to post an
      > alternative.[/color]

      OK:-

      <snip>[color=blue]
      > qty = eval('form.Line _Qty' + lineNum);[/color]

      qty = form['Line_Qty' + lineNum];
      [color=blue]
      > prc = eval('form.Line _Unit_Price' + lineNum);[/color]

      prc = form['Line_Unit_Pric e' + lineNum];
      [color=blue]
      > subt = eval('form.Line _Subtotal' + lineNum);[/color]

      subt = form['Line_Subtotal' + lineNum];

      <snip>[color=blue]
      > qty = eval('form.Line _Qty' + lineNum);[/color]

      qty = form['Line_Qty' + lineNum];
      [color=blue]
      > prc = eval('form.Line _Unit_Price' + lineNum);[/color]

      prc = form['Line_Unit_Pric e' + lineNum];
      [color=blue]
      > subt = eval('form.Line _Subtotal' + lineNum);[/color]

      subt = form['Line_Subtotal' + lineNum];

      <URL: http://jibbering.com/faq/#FAQ4_39 >

      In this context the alternative to - eval - is the standard bracket
      notation property accessor syntax, supported in every javascript
      version, shorter, simpler and between two and twenty times faster
      depending on the browser (and unlike - eval - is not optional in ECMA
      327 "Compact Profile" implementations so it will also work in more
      browsers).

      The reason that - eval - is considered a bad thing is that it really is
      the _worst_ way of doing 99.99% of the things that are done with it.

      Richard.


      Comment

      • Targa

        #4
        Re: Help with loop

        Hey! Looks like youve got me on the right track!

        But now I get 'undefined' is null or not an object.

        How can I track this buggar down?




        "Richard Cornford" <Richard@litote s.demon.co.uk> wrote in message
        news:c9nbf6$3ta $1$830fa17d@new s.demon.co.uk.. .[color=blue]
        > Shawn Milo wrote:
        > <snip>[color=green]
        > > Try something like this (untested).
        > > Some people would say that using
        > > eval() is a bad thing, so maybe
        > > one of them will take the time to post an
        > > alternative.[/color]
        >
        > OK:-
        >
        > <snip>[color=green]
        > > qty = eval('form.Line _Qty' + lineNum);[/color]
        >
        > qty = form['Line_Qty' + lineNum];
        >[color=green]
        > > prc = eval('form.Line _Unit_Price' + lineNum);[/color]
        >
        > prc = form['Line_Unit_Pric e' + lineNum];
        >[color=green]
        > > subt = eval('form.Line _Subtotal' + lineNum);[/color]
        >
        > subt = form['Line_Subtotal' + lineNum];
        >
        > <snip>[color=green]
        > > qty = eval('form.Line _Qty' + lineNum);[/color]
        >
        > qty = form['Line_Qty' + lineNum];
        >[color=green]
        > > prc = eval('form.Line _Unit_Price' + lineNum);[/color]
        >
        > prc = form['Line_Unit_Pric e' + lineNum];
        >[color=green]
        > > subt = eval('form.Line _Subtotal' + lineNum);[/color]
        >
        > subt = form['Line_Subtotal' + lineNum];
        >
        > <URL: http://jibbering.com/faq/#FAQ4_39 >
        >
        > In this context the alternative to - eval - is the standard bracket
        > notation property accessor syntax, supported in every javascript
        > version, shorter, simpler and between two and twenty times faster
        > depending on the browser (and unlike - eval - is not optional in ECMA
        > 327 "Compact Profile" implementations so it will also work in more
        > browsers).
        >
        > The reason that - eval - is considered a bad thing is that it really is
        > the _worst_ way of doing 99.99% of the things that are done with it.
        >
        > Richard.
        >
        >[/color]



        Comment

        • Lee

          #5
          Re: Help with loop

          Targa said:
          [color=blue]
          > form.Line_Qty1. value = parseInt(form.L ine_Qty1.value) ;
          > form.Line_Unit_ Price1.value = parseFloat(form .Line_Unit_Pric e1.value);
          > rowsum = form.Line_Qty1. value * form.Line_Unit_ Price1.value;[/color]

          Those first two lines are doing nothing useful.
          The value attribute of a form element is *always* a string.

          You're parsing the numeric values out of those strings,
          storing them back into the value attributes, which causes
          the engine to automatically convert them back to strings,
          then multiplying those strings, which causes them to be
          automatically converted back to numbers.

          Comment

          • Targa

            #6
            Re: Help with loop

            Actually, the loop Shawn provided is working (with Richard's mod)
            Each line totals up fine. I think its the next step (totaling all the lines)
            that's tripping me up:

            var total=0;
            var form = document.forms["calculatio ns"];
            for (var i=1; i<=<%= RS("myTotal")%> ; i++) {
            total += (form["Line_Item_Subt otal"+i].value-0);
            }
            form.subtotal.v alue = money(total);

            On page load I get 'undefined' is null or not an object.
            Once I hit the calc button, I get 'value' is null or not an object.

            It always returns the wrong line # so I dont know to to trace it.

            Complete code:
            <SCRIPT LANGUAGE="JavaS cript"><!--
            // INVOICE CALCULATIONS
            function calc(form) {
            var sum = 0;
            var rowsum;
            var quantity = 1;
            var lineNumber = 1;

            var qty;
            var prc;
            var subt;
            qty = form['Line_Qty' + lineNumber];
            prc = form['Line_Unit_Pric e' + lineNumber];
            subt = form['Line_Item_Subt otal' + lineNumber];

            while (qty.value){

            if ( parseFloat(qty. value) && parseFloat(prc. value) ) {
            quantity += parseInt(qty.va lue);
            qty.value = parseInt(qty.va lue);
            prc.value = parseFloat(prc. value);
            rowsum = qty.value * prc.value;
            sum += rowsum;
            prc.value = money(prc.value );
            subt.value = money(rowsum)
            }

            lineNumber++;
            qty = form['Line_Qty' + lineNumber];
            prc = form['Line_Unit_Pric e' + lineNumber];
            subt = form['Line_Item_Subt otal' + lineNumber];

            }

            var total=0;
            var form = document.forms["calculatio ns"];
            for (var i=1; i<=<%= RS("myTotal")%> ; i++) {
            total += (form["Line_Item_Subt otal"+i].value-0);
            }
            function money(num) // converts from floating point to money format
            {
            var amount = Math.abs(num);
            var pounds = Math.floor(amou nt);
            var pence = Math.round( 100*(amount-pounds) );
            if(pence>99) pence=0, pounds++;
            pence += ""
            while (pence.length < 2) pence = "0" + pence;
            amount = pounds + "." + pence;
            if (num < 0) return "[" + amount + "]";
            return amount;
            }

            //-->
            </SCRIPT>



            "Targa" <targa1_removet historeply_@all tel.net> wrote in message
            news:IhHvc.384$ 783.64@fe39.use netserver.com.. .[color=blue]
            > Hey! Looks like youve got me on the right track!
            >
            > But now I get 'undefined' is null or not an object.
            >
            > How can I track this buggar down?
            >
            >
            >
            >
            > "Richard Cornford" <Richard@litote s.demon.co.uk> wrote in message
            > news:c9nbf6$3ta $1$830fa17d@new s.demon.co.uk.. .[color=green]
            > > Shawn Milo wrote:
            > > <snip>[color=darkred]
            > > > Try something like this (untested).
            > > > Some people would say that using
            > > > eval() is a bad thing, so maybe
            > > > one of them will take the time to post an
            > > > alternative.[/color]
            > >
            > > OK:-
            > >
            > > <snip>[color=darkred]
            > > > qty = eval('form.Line _Qty' + lineNum);[/color]
            > >
            > > qty = form['Line_Qty' + lineNum];
            > >[color=darkred]
            > > > prc = eval('form.Line _Unit_Price' + lineNum);[/color]
            > >
            > > prc = form['Line_Unit_Pric e' + lineNum];
            > >[color=darkred]
            > > > subt = eval('form.Line _Subtotal' + lineNum);[/color]
            > >
            > > subt = form['Line_Subtotal' + lineNum];
            > >
            > > <snip>[color=darkred]
            > > > qty = eval('form.Line _Qty' + lineNum);[/color]
            > >
            > > qty = form['Line_Qty' + lineNum];
            > >[color=darkred]
            > > > prc = eval('form.Line _Unit_Price' + lineNum);[/color]
            > >
            > > prc = form['Line_Unit_Pric e' + lineNum];
            > >[color=darkred]
            > > > subt = eval('form.Line _Subtotal' + lineNum);[/color]
            > >
            > > subt = form['Line_Subtotal' + lineNum];
            > >
            > > <URL: http://jibbering.com/faq/#FAQ4_39 >
            > >
            > > In this context the alternative to - eval - is the standard bracket
            > > notation property accessor syntax, supported in every javascript
            > > version, shorter, simpler and between two and twenty times faster
            > > depending on the browser (and unlike - eval - is not optional in ECMA
            > > 327 "Compact Profile" implementations so it will also work in more
            > > browsers).
            > >
            > > The reason that - eval - is considered a bad thing is that it really is
            > > the _worst_ way of doing 99.99% of the things that are done with it.
            > >
            > > Richard.
            > >
            > >[/color]
            >
            >
            >[/color]



            Comment

            • Donald F. McLean

              #7
              Re: Help with loop

              Lee wrote:
              [color=blue]
              > Targa said:
              >
              >[color=green]
              >> form.Line_Qty1. value = parseInt(form.L ine_Qty1.value) ;
              >> form.Line_Unit_ Price1.value = parseFloat(form .Line_Unit_Pric e1.value);
              >> rowsum = form.Line_Qty1. value * form.Line_Unit_ Price1.value;[/color]
              >
              >
              > Those first two lines are doing nothing useful.
              > The value attribute of a form element is *always* a string.[/color]

              I'm not convinced that's entirely true - it does cause the values
              to be normalized.
              [color=blue]
              > You're parsing the numeric values out of those strings,
              > storing them back into the value attributes, which causes
              > the engine to automatically convert them back to strings,
              > then multiplying those strings, which causes them to be
              > automatically converted back to numbers.[/color]

              Assuming that we want the values in the fields to be normalized, is it
              less efficient to use the code above or something like this:

              var quantity = parseInt(form.L ine_Qty1.value) ;
              form.Line_Qty1. value = quantity;

              var value = parseFloat(form .Line_Unit_Pric e1.value);
              form.Line_Unit_ Price1.value = value;

              rowsum = quantity * value;

              I'm guessing that it probably doesn't matter that much.

              Donald

              Comment

              • Lee

                #8
                Re: Help with loop

                Donald F. McLean said:[color=blue]
                >
                >Lee wrote:
                >[color=green]
                >> Targa said:
                >>
                >>[color=darkred]
                >>> form.Line_Qty1. value = parseInt(form.L ine_Qty1.value) ;
                >>> form.Line_Unit_ Price1.value = parseFloat(form .Line_Unit_Pric e1.value);
                >>> rowsum = form.Line_Qty1. value * form.Line_Unit_ Price1.value;[/color]
                >>
                >>
                >> Those first two lines are doing nothing useful.
                >> The value attribute of a form element is *always* a string.[/color]
                >
                >I'm not convinced that's entirely true - it does cause the values
                >to be normalized.[/color]

                That's why I said "nothing useful", rather than absolutely nothing.
                Note that it will change a price entered as "2.10" to "2.1", which
                is probably not what they really want.

                Comment

                Working...