I am new here and currently learning JavaScript. In the code below, how can I get it to loop and still prompt if say for example the user does not enter 1 or -1? Instead of using the else statement to output "This is not a valid input," I want it to prompt the user once again until they enter the desired input. I am not quite getting the desired out with the while loop.
var myShopping = ["Eggs", "Milk", "Potatoes", "Cereal", "Banana"];
var ord = prompt("Enter 1 for alphabetical order, " +
"and -1 for reverse order", 1);
if (ord == 1) {
myShopping.sort();
document.write(myShopping.join("<br />"));
} else if (ord == -1) {
myShopping.sort();
myShopping.reverse();
document.write(myShopping.join("<br />"));
} else {
document.write("That is not a valid input");
}
document.write. :P Also,promptmakes for a horrible UI, and is arguably entirely unnecessary in an HTML page.