PDA

View Full Version : Javascript code to find whether number is positive or negative using switch statement


mik4944
Oct 12, 2010, 02:51 AM
We cannot use if statement it has o be done using switch only

objectundefined
Oct 20, 2010, 04:53 PM
When you write a "boolean statement" (i.e.: the contents of an "if" statement) outside of an "if" statement, it returns either true or false.

Your boolean statement is as follows:


(num < 0)


This can become the target of your switch like so:



switch(num < 0) {
case true:
alert('NUMBER IS NEGATIVE');
break
case false:
alert('NUMBER IS POSITIVE OR ZERO');
break
}