Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
Developer
Posts: 466
Registered: ‎04-11-2012
My Device: Bold 9900
My Carrier: Rogers
Accepted Solution

Javascript comparison bug

So I found a weird Javascript bug and was hoping someone could help me out.

 

if say

var value = 40;

var smallervalue = 8; 

 

if (value<smaller){

  doSomething;

}

 

it seems to always work for some reason even though 40 is > than 8. This bug seems to be when that javascript is only comparing the first digit of the number over the whole value of the number. I m also using an 'array' for the value for 'smaller' in my actual code. Could someone help me debug this? Many thanks!

 

 


Developer for easyDial for BlackBerry Bold & inLink for BlackBerry PlayBook
Find me online via twitter, or on the techfruits.com webpage. Please hit the like button below if what I said helped!
Developer
Posts: 51
Registered: ‎10-22-2011
My Device: 9800
My Carrier: Telus

Re: Javascript comparison bug

I am going to take a guess that you are ending up with a string comparison.   One of JS's stupider confusions.   Anyway try

 

if (+value < +smallervalue). ..

Developer
Posts: 466
Registered: ‎04-11-2012
My Device: Bold 9900
My Carrier: Rogers

Re: Javascript comparison bug

thats perfect! and works! but what does + do?


Developer for easyDial for BlackBerry Bold & inLink for BlackBerry PlayBook
Find me online via twitter, or on the techfruits.com webpage. Please hit the like button below if what I said helped!
Developer
Posts: 51
Registered: ‎10-22-2011
My Device: 9800
My Carrier: Telus

Re: Javascript comparison bug

It is just an easy shorthand for forcing a conversion to numeric.  You could also use Number(value).   The parseint function is more "correct" but also has more baggage around invalid strings.   I almost edited my answer to say that it is only correct if you are sure the strings are valid numbers.   If not, then a more thorough analysis and parseint are appropriate.

Developer
Posts: 466
Registered: ‎04-11-2012
My Device: Bold 9900
My Carrier: Rogers

Re: Javascript comparison bug

yup they are numbers for sure (the input field is numeric)


Developer for easyDial for BlackBerry Bold & inLink for BlackBerry PlayBook
Find me online via twitter, or on the techfruits.com webpage. Please hit the like button below if what I said helped!