javascript - How to stop the loop after same choices? -
My code looks something like this, but I have a problem. Whenever the options are the same, the game restarts. After finishing a game, I get an "undefined" warning, because the startGame () function again runs again with the first round parameter (where there were the same options).
I'm new to this word, and want to keep it as simple as possible. Can someone help me by providing a solution to stop 'undefined'? '
// game starts here startGame function () {// user var user chaus = prompt (" Do you choose rock, paper or scissors? "); // if user input is invalid ("userChoice! =" Rock ") & amp; amp; userChoice! =" Paper ") & amp; (userChoice! =" Scissors ")) {userChoice = prompt Choose, right now! "); } Alert ("You chose" + User Choice); // computer var computer charge = math.rendum (); If (Computer Choice <0.34) {computerChoice = "rock"; } And if (Computer Choice <= 0.67) {computerChoice = "paper"; } And {computerChoice = "scissors"; } Warning ("Choose Computer:" + computerChoice); // Comparison between user and computer = function (like 1, choice 2) {// If both choices are the same (like 1 == like 2) {Warning ("Tie, restart the game!"); game on(); } // If the user chooses rock end (like 1 == "rock") {if (choice2 == "scissors") {return "you've won the game!"; } And {return "LOOOOOSSEEER!"; }} // If the user selects scissors and if (likes 1 == "scissors") {if (choice2 == "rock") {return "You have won the game!"; }} And {return "LOOOOOSSEEER"; }}; Warning (Compare (User Chice, Computer Choice);} // startGame ();
You use a recycling here. The StartGame () function resumes and the first call of the comparison () is expected of a return statement. You do not have any return statement.
// If both choices are same (like 1 == like 2) {Warning ("Tie, restart the game!"); Start ();}
You Instead of returning the result, you can solve the alert only with the statement. Here is a JSfield (comment of the function call):
Edit:
Another way is to return the message in all cases and the startGame () function is also a return statement. JSfield here (comment of function call):
Comments
Post a Comment