Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Sep 2011
    Posts
    184
    Thanks
    140
    Thanked 0 Times in 0 Posts

    Switch Case Help

    Hi There,

    I need your help,

    I can't seem to get this work, which it theorecticly should work any ideas?

    <script>

    var x = 3

    switch(x){
    case x > 0:
    alert("overdue")
    break

    case x = 0:
    alert("due today")
    break

    case x < 0:
    alert("will be due")
    break
    }
    </script>

    Much thanks for everyones help.

    Cheers,

    Jay

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,927
    Thanks
    56
    Thanked 550 Times in 547 Posts
    I think you're kind of misusing the switch, which should be used to compare the value of a variable against another (not evaluate it relatively like you have it) it but if you really want to do it that way this would be the hack (note that I changed your case x = 0: to case x == 0: to avoid the value of x getting redefined midway through the switch):

    Code:
    <script>
    
    var x = 3;
    
    switch(true){
    case x > 0:
    alert("overdue")
    break
    
    case x == 0:
    alert("due today")
    break
    
    case x < 0:
    alert("will be due")
    break
    }
    </script>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •