-7

After asking user input , how to do a switch if answer is "yes" and another switch if answer is "no"? I believe it needs to be written as if/else, but am not sure of the format.

Question: Do you want pizza? If true: do a switch for size If false: do a switch for cake flavor

Sunny
  • 1
  • 3
  • 4
    Possible duplicate of [Switch statement multiple cases in JavaScript](https://stackoverflow.com/questions/13207927/switch-statement-multiple-cases-in-javascript) – 31piy Apr 20 '18 at 04:48
  • You can nest switches. You can also have "fall through" (not using break;) but most likely an if/ else would be preferable for readability – lucas Apr 20 '18 at 04:54

1 Answers1

0

Maybe this will give you an idea. You also could either replace else / if with a switch or the switch with else / if.

function go() {
  var input = document.getElementById('input').value;

  if (input.toLowerCase() == "yes") {
    // Here comes the switch for the size
    var size = prompt("Enter size:");
    console.log(size);
    switch (size) {
      case "20":
        alert("Okay, size will be 20");
        break;
      case "25":
        alert("Okay, size will be 25");
        break;
      default:
        alert("Size must be 20 or 25");
    }
  } else if (input.toLowerCase() == "no") {
    // And here the switch for the flavor
    var flavor = prompt("Please enter the flavor:");
    switch (flavor) {
      case "whatever":
        alert("Okay, it will taste like whatever");
        break;
      case "some":
        alert("Okay, it will taste like some");
        break;
      default:
        alert("It must taste like either whatever or some");
    }
  }
}
Do you want pizza?
<input id="input" type="text">
<button onclick="go()">Submit</button>
CodeF0x
  • 2,624
  • 6
  • 17
  • 28