View Full Version : 3 > 17 - unexpected result

09-02-2010, 07:22 AM
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:

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?

09-02-2010, 07:32 AM
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

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);

Old Pedant
09-02-2010, 08:12 AM
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( )

09-02-2010, 09:33 AM
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.

09-02-2010, 12:06 PM
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))

09-02-2010, 03:33 PM
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.

Old Pedant
09-02-2010, 08:27 PM
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.


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.

09-02-2010, 10:17 PM
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.

Old Pedant
09-03-2010, 12:18 AM
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:
There is a person who has a bright future in programming, I think.

Ah, well... Each to his/her own, I guess.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum