-1

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> 
4
  • 2
    You're comparing strings instead of numbers. The comparison isn't done the same way. Commented Oct 31, 2012 at 19:09
  • So compare numbers instead of strings if you want to? Commented Oct 31, 2012 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. Commented Oct 31, 2012 at 19:13
  • hint: "9" < "10" will always be false because "1" comes before "9" when compared alphabetically. related Commented Oct 31, 2012 at 19:48

5 Answers 5

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> 
Sign up to request clarification or add additional context in comments.

2 Comments

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".
@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').
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>');

Comments

0

You should be using numbers not strings in your comparisons.

Comments

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

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.