Skip to main content
  1. About
  2. For Teams
Asked
Viewed 91 times
-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

3
  • 1
    either ad multiple condition or use in_array
    Devsi Odedra
    –  Devsi Odedra
    2019-09-16 07:52:26 +00:00
    Commented Sep 16, 2019 at 7:52
  • Thanks alot! It's my first time working with js so thank you for the quick response!
    ksandur
    –  ksandur
    2019-09-16 07:55:19 +00:00
    Commented Sep 16, 2019 at 7:55
  • 1
    @DevsiOdedra there is no "in_array" in javascript. He can either use indexOf or includes in his case (or eventually any). in_array exists in php, not in javascript.
    briosheje
    –  briosheje
    2019-09-16 07:56:25 +00:00
    Commented Sep 16, 2019 at 7:56

2 Answers 2

0
if (day == null || day == "bad" || day == "terrible") {
    document.write("How come?");
} else {
    document.write("sounds great");
}
Sign up to request clarification or add additional context in comments.

Comments

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;
}

7 Comments

why the block statment in case statements part?
'cause I suppose he won't write just one line code.
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 no, it won't be a great question. It's primarily opinion based and should be closed as such.
@VLAZ, it is not opion based, it is in this case superfluous, because of no local variables.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.