0

I have the following code:

var t = 13;

function check() {
    if ((t >= 12) && (t < 20)) {
        alert(true);
    }
}

Which I want to convert to switch statement. I have tried this:

function test() {
    switch (t) {
        case (t >= 12) && (t <= 20):
            alert("> 12");
            break;
    }
}

But it doesn't work.I get no errors via the console.Any Ideas? Actually this question came after comparison with Pascal where i can do this:

case t of
12..19://code
end;

which is equal to

if (t>=12) and (t<=19) then
//code
cssGEEK
  • 994
  • 2
  • 15
  • 38

2 Answers2

0

switch doent work like that, you can pass true to switch and can use case statement like below

switch(true){
            case ((t >= 12) && (t <= 20)):
            alert("> 12");
            break;
        }

Note: : it is recomended to use if/else as an better option here

Working Demo :

var t = 13;

function test(){
        switch(true){
            case ((t >= 12) && (t <= 20)):
            alert("> 12");
            break;
        }
    }

test();
veducm
  • 5,933
  • 2
  • 34
  • 40
A.B
  • 20,110
  • 3
  • 37
  • 71
-2

Finaly found the answer:

function test(){
        switch(true){
            case (t >= 12) && (t <= 20):alert("> 12");
            break;

        }
    }
cssGEEK
  • 994
  • 2
  • 15
  • 38
  • 1
    How is this better than your original formulation? – Codor Mar 25 '15 at 14:15
  • 1
    Even if it works it is not a recommended operator to use in here. – Rahul Tripathi Mar 25 '15 at 14:15
  • It is working.But why downvoting? – cssGEEK Mar 25 '15 at 14:16
  • 2
    @cssGEEK Don't listen to them. This is perfectly fine. A little verbose over a simple `if` but still perfectly fine. – rgthree Mar 25 '15 at 14:19
  • @rgthree thanks.Actually my code is working – cssGEEK Mar 25 '15 at 14:20
  • @cssGEEK:- Not the downvoter as its not wrong. But people are downvoting it as its a bad approach of solving a simple thing. Downvoting doesnot always means a wrong answer but that people dont like the solution. – Rahul Tripathi Mar 25 '15 at 14:22
  • thats an fine solution i think nothing wrong with it. – A.B Mar 25 '15 at 14:23
  • @A.B _fine_ is a personal opinion then it may be OK for you but in general: 1) it's not _intended use_ for `switch` then it'll confuse MANY people (they may need to read it 2 or 3 times to understand it). Someone may even don't understand _why_ it works. 2) it's even longer and more prolix than plain `if` so I can't see a reason for it. 3) it's more error prone (not just because absolutely unreadable and counter intuitive but also because it adds a chance to introduce the _missing `break`_ bug). So many drawbacks for which reason? What's the gain? – Adriano Repetti Mar 26 '15 at 12:51
  • @AdrianoRepetti i agree with your comments and mentioned it in my note also , and i pasted answer before watching this, we both posted at same time – A.B Mar 26 '15 at 13:34
  • @A.B just saw it! Please ignore 2nd comment. – Adriano Repetti Mar 26 '15 at 13:36
  • @AdrianoRepetti yes i agree that here switch has no advantage over if/else but still a way it is to do, but NOT RECOMMENDED – A.B Mar 26 '15 at 13:41
  • @A.B absolutely, when I posted first comment I didn't see your answer and I was answering to your comment (_"...nothing wrong with it"_). – Adriano Repetti Mar 26 '15 at 13:58
  • okay :) well your comments have got me downvote, thanks – A.B Mar 26 '15 at 14:06