-1

I used if, else if, and else statements before and today I've decided to use switch statements which really simplified my code. My question is, just like if statements, is there a way I can add multiple conditions inside a switch statement?

Here's an example:

<script>
var textInput = input.value;

switch (textInput) {
case "orange":
    text = "You decided to eat an orange. Do you want to eat another fruit?";

}
document.getElementById("message").innerHTML = text;

</script>

But say I wanted to add a response to the question I added about if you wanted to eat another fruit. How would I add another condition inside that case to where I could get a response back if someone types in yes or no to the question?

Is something like that possible? I hope my question is easy to understand.

Thanks for your help!

  • Add another `case`? If you want to handle "apple", then do `case "apple": text = "something with apple";` – VLAZ Apr 06 '20 at 22:30
  • Just add a new case, and don't forgot to put break statement in previous case, else it will run all the case from first matching case to last case – Code Maniac Apr 06 '20 at 22:33
  • For future reference OP, the [MDN web docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch) is a great place to learn more about Javascript. – OSH Apr 06 '20 at 22:35
  • once the code of your switch or if / else If is done and sent to the client browser, you can no longer intervene on the switch or if / else If. There are other possible ways to code what you still want should explain the logic on the text in response – Mister Jojo Apr 06 '20 at 22:52

7 Answers7

1

You can put any ordinary code inside the case, so you can add if statements:

switch (textInput) {
case "orange":
    if (some_other_condition) {
        text = "You decided to eat an orange. Do you want to eat another fruit?";
    } else {
        text = "OK, that's the last fruit";
    }
    break;
    ...
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

Absolutely!! Adding to your code:

<script>
var textInput = input.value;

switch (textInput) {
case "orange":
    text = "You decided to eat an orange. Do you want to eat another fruit?";
    break;
case "banana":
    text = "Had a banana today";
    break;
case default: // Which cannot be found in the case will drop to here
    text : ""; // DO nothing as per OP request
    break;
}
document.getElementById("message").innerHTML = text;

</script>

For more details, please refer: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch

LearningEveryday
  • 584
  • 1
  • 3
  • 13
  • Hey thanks for the response! While this does work, it's not really helpful for me when I have multiple switch statements. Because if I wasn't even in the topic of fruits and I randomly typed in banana, it'll still give me the outcome of banana which is "hava a banana day". I'm looking for something like, if you typed in banana when you weren't in the topic of fruit, nothing would happen.. But if you typed in orange, and it gave you the question, do you want to eat another fruit? Then typed bananas, only then would that outcome occur. – The.Epistemophile.Bibliophile Apr 06 '20 at 22:48
0

you can do something like this although in general it's not the way to go:

function test(input1, input2) {
     switch (true) {
        case input1 > input2:
                    console.log(input1 + " is larger than " + input2);
                    break;
        case input1 < input2:
                    console.log(input2 + " is larger than " + input1);
                    break;
        default:
                    console.log(input1 + " is equal to " + input2);
      }
   }
   
   test(5,6);
DCR
  • 14,737
  • 12
  • 52
  • 115
0

As long as you don't break the case, it will continue through the switch statement.

switch (textInput) {
  case "orange":
    text = "You decided to eat an orange. Do you want to eat another fruit?";
    // break; we won't break here since it's commented out
  case "fruit":
    text = "You decided to eat a fruit?";
    break;
}

will assign You decided to eat a fruit? to text if textInput is orange or fruit.

This gives you a limited ability to merge certain context, but it is a very bad practice to do so.

Daniel F
  • 13,684
  • 11
  • 87
  • 116
0

Yes, you can put If conditions inside a switch, try it here.

switch (true) {
    case (dog === 'pet' && cat === 'pet'):
        // execute
        break;
    case (foo === 'bar'):
        // do something else
        break;
    default:
        // text = "You decided to eat an orange. Do you want to eat another fruit?";
}
Transformer
  • 6,963
  • 2
  • 26
  • 52
0

Well, as far as I know you can't add conditions into switch statement. Switch statement just work to catch different states or 'cases' that one variable can take.

Maybe you can try with something like:

var addMore = true
while(addMore){
 var textInput = prompt("Select your fruit");
 switch (textInput) {
  case "orange":

   addMore = confirm("You decided to eat an orange. Do you want to eat another fruit?") // Maybe a yes or no question
  default:
    addMore = false
  }
 }

But if you have a UI you should use a multiple input component like Checkbox or something like that.

I hope it was helpful

Brian Nieto
  • 273
  • 7
  • 17
0

You can insert the switch statement in a do-while loop, so the question loops until some predefined termination string is entered which will terminate the loop. This way you don't need multiple conditions inside each switch statement.

Check and run below:

var text = "Pick a fruit you'd like to eat: apple, orange, banana or none if you don't want any more";
var eaten = [];

do {
  var textInput = prompt(text);
  
  switch (textInput) {
  case "orange":
    eaten.push("orange");
    text = "You decided to eat an orange. If you'd like to eat more type apple, orange, banana or none to end";
    break;
  case "apple":
    eaten.push("apple");
    text = "You decided to eat an apple. If you'd like to eat more type apple, orange, banana or none to end";
    break;
  case "banana":
    eaten.push("banana");
    text = "You decided to eat an banana. If you'd like to eat more type apple, orange, banana or none to end";
    break;
  default:
    text = "The selection made was not valid\n\nPick a fruit you'd like to eat: apple, orange, banana or none if you don't want any more";
    break;
  }
} while(textInput !== "none");

var msgText = "You ate: ";
for(var i=0; i<eaten.length; i++) {
  msgText += eaten[i];
  if(i != eaten.length-1)
    msgText += ", ";
}
document.getElementById("message").innerHTML = msgText;
<div id="message"></div>
Ivan86
  • 5,695
  • 2
  • 14
  • 30