JavaScript can't do math!?!?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jonathyn Bet'nct

    #1

    JavaScript can't do math!?!?

    Does anybody know why these functions seem to be multiplying things by
    10 or 100? It must be something obvious, but I can't track it down.

    function multiConvert(n, spra, sm, spoa, dpra, dm, dpoa, inv) {
    var z;
    if (inv == 1) {
    z = ((n+spra)/sm)+spoa;
    return ((z-dpoa)*dm)-dpra;
    } else {
    z = ((n+spra)*sm)+s poa;
    return ((z-dpoa)/dm)-dpra;
    }
    }
    function doConvert(idx, max) {
    var i, pa1, m1, po1, n1, pa2, m2, po2, n2, inv;
    inv = document.forms[1].elements[0].value;
    n1 = document.forms[1].elements[idx*4 + 4].value;
    pa1 = document.forms[1].elements[idx*4 + 1].value;
    m1 = document.forms[1].elements[idx*4 + 2].value;
    po1 = document.forms[1].elements[idx*4 + 3].value;
    if (n1 != "") {
    for (i = 0; i <= max; i++) {
    if (i != idx) {
    pa2 = document.forms[1].elements[i*4 + 1].value;
    m2 = document.forms[1].elements[i*4 + 2].value;
    po2 = document.forms[1].elements[i*4 + 3].value;
    n2 = multiConvert(n1 ,pa1,m1,po1,pa2 ,m2,po2,inv);
    document.forms[1].elements[i*4 + 4].value = n2;
    }
    }
    }
    }

    --
    Hasta la pasta,
    Jonathyn Bet'nct


    -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
    http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
    -----== Over 100,000 Newsgroups - 19 Different Servers! =-----
  • Michael Winter

    #2
    Re: JavaScript can't do math!?!?

    On Thu, 26 Aug 2004 23:51:00 -0700, Jonathyn Bet'nct
    <jonrelay@napan et.net> wrote:
    [color=blue]
    > Does anybody know why these functions seem to be multiplying things by
    > 10 or 100? It must be something obvious, but I can't track it down.[/color]

    Probably[1] because you're concatenating strings, rather than adding
    numbers. The value property of form elements holds strings. Whilst
    subtraction, division, and multiplication operations between two strings
    results in the automatic conversion to number, no such conversion occurs
    with addition (one operand would have to be a number).

    The solution is to force conversion for all form element values that
    represent numbers. If the form elements are text boxes, you should ensure
    that the values are actually numbers before converting. This is best
    accomplished with regular expressions.

    Simple conversion to number is through the unary plus operator. For
    example,

    inv = document.forms[1].elements[0].value;
    if( <inv is not number> ) {
    // exit function
    }
    inv = +inv; // convert to number

    [snipped multiConvert]
    [color=blue]
    > function doConvert(idx, max) {
    > var i, pa1, m1, po1, n1, pa2, m2, po2, n2, inv;
    > inv = document.forms[1].elements[0].value;
    > n1 = document.forms[1].elements[idx*4 + 4].value;
    > pa1 = document.forms[1].elements[idx*4 + 1].value;
    > m1 = document.forms[1].elements[idx*4 + 2].value;
    > po1 = document.forms[1].elements[idx*4 + 3].value;[/color]

    You would be better off storing a reference to the form, rather than
    performing a look up again, and again.

    var form = document.forms[1],
    inv = form.elements[0].value,
    n1 = form.elements[idx*4 + 4].value;
    // etc

    [snipped rest of function]

    Hope that helps,
    Mike


    [1] A URI to a working example would make certain.

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.

    Comment

    • Jonathyn Bet'nct

      #3
      Re: JavaScript can't do math!?!?

      Michael Winter <M.Winter@bluey onder.co.invali d> wrote:
      [color=blue]
      > On Thu, 26 Aug 2004 23:51:00 -0700, Jonathyn Bet'nct
      > <jonrelay@napan et.net> wrote:
      >[color=green]
      > > Does anybody know why these functions seem to be multiplying things by
      > > 10 or 100? It must be something obvious, but I can't track it down.[/color]
      >
      > Probably[1] because you're concatenating strings, rather than adding
      > numbers. The value property of form elements holds strings. Whilst
      > subtraction, division, and multiplication operations between two strings
      > results in the automatic conversion to number, no such conversion occurs
      > with addition (one operand would have to be a number).
      >
      > The solution is to force conversion for all form element values that
      > represent numbers. If the form elements are text boxes, you should ensure
      > that the values are actually numbers before converting. This is best
      > accomplished with regular expressions.
      >
      > Simple conversion to number is through the unary plus operator.[/color]

      Thank you. I found out late last night that it was because of
      concatenation, but I solved it by multiplying by 1. The unary plus
      operator is probably better, so I changed it to use that.
      [color=blue]
      > You would be better off storing a reference to the form, rather than
      > performing a look up again, and again.[/color]

      I've also done this. It appears to be a little faster.
      [color=blue]
      > Hope that helps,
      > Mike[/color]

      It does, definitely. Thank you so much.
      [color=blue]
      > [1] A URI to a working example would make certain.[/color]



      The server has currently exceeded bandwidth, so here's another URL that
      might work:



      --
      Hasta la pasta,
      Jonathyn Bet'nct


      -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
      http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
      -----== Over 100,000 Newsgroups - 19 Different Servers! =-----

      Comment

      Working...