How can I prevent console from printing in JavaScript game? -
i've been practicing game javascript, , wondering how prevent console printing when user makes ill defined choice?
here code:
var user = prompt("do choose rock, paper or scissors?"); var computer = math.random(); if (computer < 0.34) { computer = "rock"; } else if (computer <= 0.67) { computer = "paper"; } else { computer = "scissors"; } console.log("computer chooses: " + computer); console.log("user chooses: " + user); var compare = function (computer, user) { if (computer === "rock") { if (user === "scissors") { return "computer wins choosing rock!"; } } else if (computer === "scissors") { if (user === "paper") { return "computer wins choosing scissors!"; } } else if (computer === "paper") { if (user === "rock") { return "computer wins choosing paper!" } } if (computer === user) { return ("it tie!") } else if (user === "paper") { if (computer === "rock") { return ("you win choosing paper!") } } else if (user === "rock") { if (computer === "scissors") { return ("you win choosing scissors!") } } else if (user === "scissors") { if (computer === "paper") { return ("you win choosing scissors!") } } ***if (user !== "rock" && user !== "paper" && user !== "scissors") { confirm(user + " invalid entry."); }*** }; compare(computer, user);
at end snipped bit of code gives user indication has put in wrong characters. wondering is:
how keep displaying console once has put in wrong input?
one option keep asking user valid input until valid input given:
while (user != "rock" && user != "paper" && user != "scissors") { user = prompt("do choose rock, paper or scissors?") if (user == null) { break; } }; if (user != null) { ... }
Comments
Post a Comment