Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-02-2010, 06:22 AM   PM User | #1
rotezecke
New Coder

 
Join Date: Sep 2010
Posts: 20
Thanks: 3
Thanked 1 Time in 1 Post
rotezecke is an unknown quantity at this point
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?
rotezecke is offline   Reply With Quote
Old 09-02-2010, 06:32 AM   PM User | #2
Lerura
Regular Coder

 
Lerura's Avatar
 
Join Date: Aug 2005
Location: Denmark
Posts: 869
Thanks: 0
Thanked 112 Times in 111 Posts
Lerura will become famous soon enough
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);
     }
....
}
Lerura is offline   Reply With Quote
Users who have thanked Lerura for this post:
rotezecke (09-02-2010)
Old 09-02-2010, 07:12 AM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,567
Thanks: 62
Thanked 4,058 Times in 4,027 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 09-02-2010, 08:33 AM   PM User | #4
Lerura
Regular Coder

 
Lerura's Avatar
 
Join Date: Aug 2005
Location: Denmark
Posts: 869
Thanks: 0
Thanked 112 Times in 111 Posts
Lerura will become famous soon enough
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.
Lerura is offline   Reply With Quote
Old 09-02-2010, 11:06 AM   PM User | #5
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,907
Thanks: 10
Thanked 293 Times in 289 Posts
Dormilich is on a distinguished road
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))
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 09-02-2010, 02:33 PM   PM User | #6
Lerura
Regular Coder

 
Lerura's Avatar
 
Join Date: Aug 2005
Location: Denmark
Posts: 869
Thanks: 0
Thanked 112 Times in 111 Posts
Lerura will become famous soon enough
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.
Lerura is offline   Reply With Quote
Old 09-02-2010, 07:27 PM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,567
Thanks: 62
Thanked 4,058 Times in 4,027 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 09-02-2010, 09:17 PM   PM User | #8
Lerura
Regular Coder

 
Lerura's Avatar
 
Join Date: Aug 2005
Location: Denmark
Posts: 869
Thanks: 0
Thanked 112 Times in 111 Posts
Lerura will become famous soon enough
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.
Lerura is offline   Reply With Quote
Old 09-02-2010, 11:18 PM   PM User | #9
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,567
Thanks: 62
Thanked 4,058 Times in 4,027 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:26 PM.


Advertisement
Log in to turn off these ads.