1

How can I write this if..else statement with a switch statement in JavaScript?

var hour = new Date().getHours();
var msg = "";
if (hour >= 0 && hour < 6) {
    msg = "بامداد شما بخیر !";
} else if (hour >= 6 && hour < 11) {
    msg = "صبح شما بخیر !";
} else if (hour >= 11 && hour < 15) {
    msg = "ظهر شما بخیر !";
} else if (hour >= 15 && hour < 20) {
    msg = "عصر شما بخیر !";
} else if (hour >= 20 && hour < 24) {
    msg = "شب شما بخیر !";
} else {
    msg = "ساعت وارد شده نامعتبر است !";
}
print(hour);
print("------------------------");
print(msg);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mehdi
  • 31
  • 2
  • possible duplicate of [Java switch statement multiple cases](http://stackoverflow.com/questions/5086322/java-switch-statement-multiple-cases) – HerpDerpington Sep 27 '15 at 12:40

2 Answers2

3

Since hour has just 24 discrete values, you could just handle all of them in a switch-case:

switch (hour) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
    msg = "بامداد شما بخیر !";
    break;
case 6:
case 7:
case 8:
case 9:
case 10:
    msg = "صبح شما بخیر !";
    break;
case 11:
case 12:
case 13:
case 14:
    msg = "ظهر شما بخیر !";
    break;
case 15:
case 16:
case 17:
case 18:
case 19:
    msg = "عصر شما بخیر !";
    break;
default:
    msg = "شب شما بخیر !";
}

But honestly, isn't a wasted effort. Using if-else is much more appropriate.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
1

I'll just say at the outset that I wouldn't rewrite it as a switch. But I would definitely change it. (I'll do the switch below, though.)

First, your "ساعت وارد شده نامعتبر است !" case will never happen. getHours returns a number between 0 and 23, inclusive. So there's no need to handle the case of < 0 or >= 24.

Here's how I'd write it:

var hour = new Date().getHours();
var msg;
if (hour < 6) {
    msg = "بامداد شما بخیر !";
} else if (hour < 11) {
    msg = "صبح شما بخیر !";
} else if (hour < 15) {
    msg = "ظهر شما بخیر !";
} else if (hour < 20) {
    msg = "عصر شما بخیر !";
} else {
    msg = "شب شما بخیر !";
}
print(hour);
print("------------------------");
print(msg);

Note how we're not having to write our limits in two places. There's no need for it, since we won't evaluate later else ifs if the earlier if was true.

Now, for the actual switch, we do something similar, using the fact that JavaScript's switch is more powerful than in many similar-looking languages (JavaScript's switch is basically just an if/else if):

var hour = new Date().getHours();
var msg;
switch (true) {
    case hour < 6:
        msg = "بامداد شما بخیر !";
        break;
    case hour < 11:
        msg = "صبح شما بخیر !";
        break;
    case hour < 15:
        msg = "ظهر شما بخیر !";
        break;
    case hour < 20:
        msg = "عصر شما بخیر !";
        break;
    default:
        msg = "شب شما بخیر !";
        break;
}
print(hour);
print("------------------------");
print(msg);

How that works: JavaScript's switch allows the case labels to be expressions. It evaluates each case in source code order against the value you give in the switch, taking the first case that matches. If no cases match, it uses the default (regardless of where the default is in the source code).

Again: I wouldn't do that. But the question was how to do it. :-)

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875