-3

I have made my first js script and I was wondering, is it possible to add multiple values to a "if var =="

var day = window.prompt("How was your day? Describe it in 1 word!");

if (day == null, "bad", "terrible") {
    document.write("How come?");
} else {
    document.write("sounds great");
}

I expect it to give a different response ONLY if the var day is bad or terrible. Not for any other responses!

But the output is only

6502
  • 112,025
  • 15
  • 165
  • 265

2 Answers2

0
if (day == null || day == "bad" || day == "terrible") {
    document.write("How come?");
} else {
    document.write("sounds great");
}
Puwka
  • 640
  • 5
  • 13
0

You could use the switch statement.

switch (day) {
  case "bad":
  case "terrible":{ document.write("How come?"); } break;
  default: { document.write("sounds great"); } break;
}

So, in you case:

var day = window.prompt("How was your day? Describe it in 1 word!");
switch (day) {
   case "bad":
   case "terrible":{ document.write("How come?"); } break;
   default: { document.write("sounds great"); } break;
}
Alessandro
  • 4,382
  • 8
  • 36
  • 70
  • why the block statment in `case` statements part? – Nina Scholz Sep 16 '19 at 08:05
  • 'cause I suppose he won't write just one line code. – Alessandro Sep 16 '19 at 08:07
  • [What do the curly braces do in switch statement after case in es6?](https://stackoverflow.com/questions/42480949/what-do-the-curly-braces-do-in-switch-statement-after-case-in-es6) I've always used it, I don't know if its a good practice, this could be a great question. – Alessandro Sep 16 '19 at 08:09
  • @Alessandro no, it won't be a great question. It's primarily opinion based and should be closed as such. – VLAZ Sep 16 '19 at 08:12
  • @VLAZ, it is not opion based, it is in this case superfluous, because of no local variables. – Nina Scholz Sep 16 '19 at 08:15
  • @NinaScholz coding styles might prefer it even if superfluous, however. Yes, it's *pointless* but since it is, you are free to require it as there won't be any change in behaviour. Coding style is all about that - requiring one of multiple options when there is no functional difference. And coding styles are just opinions, ergo, if a coding style would make you choose something, it's POB. – VLAZ Sep 16 '19 at 08:17
  • @NinaScholz I always close my code into braces, even when it's just one line, I think it's cleaner, especially because you can add new lines without lose scope variables and it's better to read code versioning diff. – Alessandro Sep 16 '19 at 08:21