Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Javascript (https://www.askmehelpdesk.com/forumdisplay.php?f=452)
-   -   Javascript code to find whether number is positive or negative using switch statement (https://www.askmehelpdesk.com/showthread.php?t=515870)

  • Oct 12, 2010, 02:51 AM
    mik4944
    Javascript code to find whether number is positive or negative using switch statement
    We cannot use if statement it has o be done using switch only
  • Oct 20, 2010, 04:53 PM
    objectundefined
    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:
    Code:

    (num < 0)
    This can become the target of your switch like so:

    Code:

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


  • All times are GMT -7. The time now is 05:41 AM.