Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Sep 2010
    Posts
    20
    Thanks
    3
    Thanked 1 Time in 1 Post

    3 > 17 - unexpected result

    Hi there,
    i have a dynamic form that is populated via a sql query (WHERE $AVAILABLE_PRODUCT > 0)

    hence the javascript that is called onchange has dynamic field names. i used numbers.
    the for loop works fine:

    Code:
    var numberOfFields = document.order_form.elements.length;
    for (var i=1; i<numberOfFields-3; i=i+4)
    {
       if (document.order_form.elements[i].value > document.order_form[i+3].value)
         {
           alert(document.order_form.elements[i].value + ' > ' + document.order_form[i+3].value);
         }
    ....
    }
    Alert Box shows: 3 > 17 or similar (where 17 is the number of items on stock, and three the number of items ordered.)
    all other calculations with that form work fine.
    any idea?

  • #2
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    947
    Thanks
    0
    Thanked 130 Times in 129 Posts
    your values are probably passed to the function as strings, not numbers.
    If so the function compares then alphabetically. and then 3 is higher than 17

    to solve that
    Code:
    var numberOfFields = document.order_form.elements.length;
    for (var i=1; i<numberOfFields-3; i=i+4)
    {
       if (document.order_form.elements[i].value*1 > document.order_form[i+3].value*1)
         {
           alert(document.order_form.elements[i].value + ' > ' + document.order_form[i+3].value);
         }
    ....
    }

  • Users who have thanked Lerura for this post:

    rotezecke (09-02-2010)

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,688
    Thanks
    80
    Thanked 4,650 Times in 4,612 Posts
    Quote Originally Posted by lerura View Post
    your values are probably passed to the function as strings, not numbers.
    Not "probably". By DEFINITION.

    *ALL* form field values are *ALWAYS* string. If you see .value that's a red flag that you *ARE* getting a string and only a string.


    Multiplying by 1 works, but a probably better approach is to use the builtin function, parseFloat( )
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    947
    Thanks
    0
    Thanked 130 Times in 129 Posts
    The asker was satisfied with the answer, and therefore there is no reason for you to search for the such unimportant thing to comment on

    And I know of parsefloat.
    But in most cases times 1 is sufficient, and then there is no reason for adding those extra characters to the file.

    And in the future:
    Try focusing on the question, and help the asker, instead of trying to correct the other helpers.

  • #5
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,474
    Thanks
    13
    Thanked 361 Times in 357 Posts
    there is a difference between doing something and understanding, why you do something (or doing something and doing something right (and maybe even doing something right efficiently))
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #6
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    947
    Thanks
    0
    Thanked 130 Times in 129 Posts
    I chose to add the word "probably" because I wasn't 100% sure, that it is still a "no-exception".

    I explained the asker that the problem with the comparison was (unless anything have changed -see above) that the script compared strings and not numbers, and that to convert the data into a number was done, and is most cases sufficiently, by multiplying by 1.

    And I am pretty sure, that the asker understood it that way.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,688
    Thanks
    80
    Thanked 4,650 Times in 4,612 Posts
    Sheesh. Look, I always treat any question as a learning experience for the one who asked it.

    Yes, you solved his problem.

    But the next time he has a similar problem, he's liable to come back and get another answer. UNLESS he understands the cause of the problem.

    And the reason I prefer parseFloat is simple: It will give me a NaN value that I can test for and then I can popup an alert or whatever to tell the user to correct his/her input. Could I do the same by multiplying by 1? Okay, I guess so. But in any case, I would probably advice the person asking the question to test for a number before *assuming* that it's valid.

    Now...

    Notice my user name: OldPedant. Okay, I readily admit the name fits. Somebody tagged me with it once, and it stuck. However, I'll make an effort to remember your username and not comment on your posts.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    947
    Thanks
    0
    Thanked 130 Times in 129 Posts
    Quote Originally Posted by Old Pedant View Post
    However, I'll make an effort to remember your username and not comment on your posts.
    That's not what i asked for!
    That you comment on my posts, is not an issue itself.
    But when we are going help newbies or less experienced, I think we must teach them at their level, not ours.

    A little like in highschool:
    If they have problems with multiplication, then trying to teach them about combinations, permutations, or maybe integrals and complex numbers, would be nothing but nonsense to them. even factorials would.

    Of course he can always google "parseFloat" to see what that build-in function actually does.

    Mostly you can see what level they are by the type of issue that they have, and the way they ask, and then it up to us to try to match our teaching with that level.

    And correcting each other at a more advanced level, could cause confusion instead of solution.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,688
    Thanks
    80
    Thanked 4,650 Times in 4,612 Posts
    I suppose you make valid points.

    I admit I remember trying to help a woman in Australia learn ASP coding. I tried for maybe 3 years. And by the end of the second year, she was still asking the same questions she did at the very first. She simply was incapable of learning the concepts involved. So for the last year I gave up and just answered questions.

    Still, I always keep hoping that even newbies are capable of learning the concepts.

    And every now and then you *do* run across such people. Witness this thread from the last couple of days:
    http://www.codingforums.com/showthread.php?t=203704
    There is a person who has a bright future in programming, I think.

    Ah, well... Each to his/her own, I guess.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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