1

I'm trying to learn how to do a switch statement using javascript. Can you guys help me how to convert this one into switch statement?

        if (x == ix && y == iy){//should be the default 
            x.style.backgroundColor = 'white'; 
        }
        if(x < ix){
            x.style.backgroundColor = 'red'; 
        }
        else if(x > ix){
            x.style.backgroundColor = 'blue';  
        }
        if(y < iy){
            x.style.backgroundColor = 'green'; 
        }
        else if(y > iy){
            x.style.backgroundColor = 'yellow';  
        }
4
  • I wouldn't rewrite this using a switch statement, and it isn't really possible either. By the way if x == ix but y != iy then you may have a case which sort of falls through the cracks. You should double check your logic. Commented Nov 18, 2017 at 7:21
  • have you made a little graphic about the result? the values of y have a higher priority over x. Commented Nov 18, 2017 at 8:45
  • What are x, y, ix and iy ? Commented Nov 29, 2017 at 20:48
  • @TimBiegeleisen Why not ? Commented Nov 29, 2017 at 20:48

2 Answers 2

1

JavaScript does not support operations other than strict equality in switches. In other words, you cannot write that program as a switch.

In a switch, you can compare a variable to different values (or cases) and check if they are equal. If they are, you execute the code given under the case.

There is a drawback, however, and it is that you can convert this code into a switch easily:

if (a === 1) {
  console.log("one");
} else if (a === 2) {
  console.log("two");
} else {
  console.log("Out of range! :(");
}

The above code in switch is

switch (a) {
  case 1:
    console.log("one");
    break;

  case 2:
    console.log("two");
    break;

  default:
    console.log("Out of range! :(");
    break;
}

But you cannot do the same to a code that contains relational operations.

switch (a) {
  case > 1: // throws error
    doSomething();
    break;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Well... here's something that you might find surprising jsfiddle.net/DerekL/jpw147pr
I see i came from php backend just trying if javascript can do such a php
@Derek朕會功夫, that's interesting, but unfortunately, that's not what I mean here.
@KarthaCoder I get your point and I was just sharing an interesting piece of code ;) I'm actually not sure whether it should be used in production.
0

Hope this could be helpful to convert your above conditions.

switch (new Date().getDay()) {
    case 0:
        day = "Sunday";
        break;
    case 1:
        day = "Monday";
        break;
    case 2:
        day = "Tuesday";
        break;
    case 3:
        day = "Wednesday";
        break;
    case 4:
        day = "Thursday";
        break;
    case 5:
        day = "Friday";
        break;
    case 6:
        day = "Saturday";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.