JavaScript var=key not working? -


anybody have idea happening i've got code

console.log('ccp: '+chatcurrentplace+' - key: '+key);  if(key>chatcurrentplace){chatcurrentplace=key;}  console.log('ccp: '+chatcurrentplace+' - key: '+key); 

and console logs

ccp: 0 - key: 4  ccp: 4 - key: 4  ccp: 4 - key: 7  ccp: 7 - key: 7  ccp: 7 - key: 8  ccp: 8 - key: 8  ccp: 8 - key: 9  ccp: 9 - key: 9  ccp: 9 - key: 11  ccp: 9 - key: 11  

why last 1 not working? should ccp: 11 - key: 11

one or both of variables strings, being compared strings , no numbers. "9" > "11" same reason "b" > "aa" (strings compared character character until first index differ).

convert values numbers in test (e.g. unary + operator) :

if( +key > +chatcurrentplace ){ chatcurrentplace = key; }  

or the parseint function:

if( parseint(key, 10) > parseint(chatcurrentplace, 10) ){ chatcurrentplace = key; }  

you may wish convert values before reaching if remain numbers throughout.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -