0

I'm having an issue with this basic conditional statement..

When the var equals 9 then the script works but once hits 10 and above it refers to "running low" which is only for the number 5 and below...please help..

Thank you.

<script type="text/javascript">


var inventory = "9";


if( inventory == "Sold Out" ){
   document.write("<b>Sold Out</b>");


}else if( inventory >= "6" ){
   document.write("<b>more than 5</b>");

}else if( inventory <= "5" ){
   document.write("<b>running low</b>");

}else{
  document.write("<b>error</b>");
}

</script> 
  • 2
    You're comparing strings instead of numbers. The comparison isn't done the same way. – I Hate Lazy Oct 31 '12 at 19:09
  • So compare numbers instead of strings if you want to? – Bergi Oct 31 '12 at 19:09
  • I removed the quotes from 6 and 5 but kept it on the 9 because it can be a string and not always an integer. – raffi keklikian Oct 31 '12 at 19:13
  • hint: `"9" < "10"` will always be false because "1" comes before "9" when compared alphabetically. [related](http://stackoverflow.com/questions/10863092/why-is-string-11-less-than-string-3) – jbabey Oct 31 '12 at 19:48

5 Answers5

2

You should compare numbers instead of string, and hence your condition gets wrong, try this

<script type="text/javascript">
var inventory = 9;
  if( inventory == "Sold Out" ){
     document.write("<b>Sold Out</b>");
  }
  else if( inventory >= 6 ){
   document.write("<b>more than 5</b>");
  }
  else if( inventory <= 5 ){
   document.write("<b>running low</b>")
  }
  else{
    document.write("<b>error</b>");
 }
</script> 
Mr. Alien
  • 153,751
  • 34
  • 298
  • 278
  • Note that the OP is passing in strings (based on the first if statement), your solution (as with mine) will also work if inventory == "10". – Korvin Szanto Oct 31 '12 at 19:12
  • @raffi keklikian in addition to this code, you may also want to consider checking if the value is a string (`typeof(inventory)=='string'`) or a number (`typeof(inventory)=='number'`). – Joshua Dwire Oct 31 '12 at 19:13
1

As others have pointed out, the problem is that when numbers are quoted they have different meaning then when unquoted.

For example:

var a = 2;

console.log(a == "2"); //returns true
console.log(a === "2"); //returns false

I recommend using 'strict' operators in JavaScript in most cases.

Documentation can be found here.


Also, you may want to consider using a switch statement, as it is easier to read and maintain.

var feedback,
    inventory = 9;

switch (true) {
    case inventory === 'Sold Out' : feedback = 'Sold Out'; break;
    case inventory >= 6           : feedback = 'more than 5'; break;
    case inventory <= 5           : feedback = 'running low'; break;
    default : feedback = 'error'; break;
}

document.write('<b>' + feedback + '</b>');
losthorse
  • 1,530
  • 1
  • 13
  • 33
0

You should be using numbers not strings in your comparisons.

HBP
  • 15,685
  • 6
  • 28
  • 34
0

Your problem is the fact that you're comparing strings rather than numbers, try using inventory >= 5

Example: http://jsfiddle.net/UwGGg/

var inventory = "10";


if ( inventory == "Sold Out" ){
   document.write("<b>Sold Out</b>");


} else if ( inventory >= 6 ){
   document.write("<b>more than 5</b>");

} else if ( inventory <= 5 ){
   document.write("<b>running low</b>");

} else {
  document.write("<b>error</b>");
}​
Korvin Szanto
  • 4,531
  • 4
  • 19
  • 49
0

If you want to be expressly clear that your variable (inventory) can be a string OR a number (seems you will accept the value "Sold Out"), I recommend forcing it into a number for comparison... This should reduce/eliminate unexpected results (if you do unit testing) and provide some code documentation for somebody else to key them off that you have are testing a value that is KNOWN to be either string or number.

rogodeter
  • 460
  • 9
  • 19