raffi keklikian
raffi keklikian

Reputation: 3

Javascript if statement issue

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> 

Upvotes: -1

Views: 90

Answers (5)

losthorse
losthorse

Reputation: 1570

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>');

Upvotes: 1

rogodeter
rogodeter

Reputation: 470

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.

Upvotes: 0

Korvin Szanto
Korvin Szanto

Reputation: 4501

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>");
}​

Upvotes: 0

HBP
HBP

Reputation: 16033

You should be using numbers not strings in your comparisons.

Upvotes: 0

Mr. Alien
Mr. Alien

Reputation: 157334

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> 

Upvotes: 2

Related Questions