User input and multiple switch statements

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>

Leave a Comment