Вы можете вставить оператор switch
в do-while
l oop, поэтому вопрос повторяется до тех пор, пока не будет введена какая-то предопределенная строка завершения, которая завершит l oop. Таким образом, вам не нужно несколько условий внутри каждого оператора switch
.
Проверьте и запустите ниже:
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>