• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Number comparison...incorrect result

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Help please!

The value stored in my variables are as follows:
sentAmount = 1000;
transferComms[i+1] = 400;

despite this, the comparison below, always returns false.
if ( (sentAmount >= transferComms[i+1]) ){


This also, alert( (sentAmount >= transferComms[i+1]) ); always returns false

Can you please tell me what is causing this and how I can get round it.

Thank you.

'Dele Oke.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there any other code between the assigments and the comparison?
 
Tokunbo Oke
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reponse.

Actually, I did not display the exact code. The exact code is:



var transferComms = new Array();

function getTransferComms(fromCurrency, sentAmount){
var transferComm = "";

if ( !itsEmpty(sentAmount) ){
for (i = 1; i <= transferComms.length-4; i += 4){
if ( (transferComms[i] == fromCurrency) ){
if ( (sentAmount >= transferComms[i+1]) ){
if ( (sentAmount <= transferComms[i+2]) ){
transferComm = transferComms[i+3];
break;
}
}
}
}
}
else{
alert("Please enter sent amount.");
}
return transferComm;
}


The code is in a .js file. The array slots are set to a list of values, some of which are strings and some of which are numbers. As you would have noticed, sentAmount is passed to the function at call time. With alerts I have confirmed that value of the parameter and array slot are as follows:


sentAmount is 1000 and
transferComms[i+1] is 400, yet the comparison returns the opposite of what is expected. I have a feeling it is doing a string comparison, instead of number. If this is the case, how can I force it to do number comparison?

Thank you.

'Dele Oke
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
are you sure you do not want i=0 and not i=1

Eric
 
Tokunbo Oke
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the question.

I do want i to be initialised to 1, not 0.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If some of your numbers are actually strings, you might want to convert all of them to numbers, because for strings it is true that "400" > "1000". The comparison is done character by character, and "4" > "1".
 
Try 100 things. 2 will work out, but you will never know in advance which 2. This tiny ad might be one:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic