Is there a "goto" function to start the game over?

project
game

#1

Hello all. My kids seem to love this little game now that it's finished. As you can see I've changed a few things in the code to make the print out a bit more fun. I was curious if there is a GoTo type of tag/function in java that I can propmt to play again and if answered yes or confirmed, to then loop back and start over? Thank you.

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
	computerChoice = "rock";
} else if(computerChoice <= 0.67) {
	computerChoice = "paper";
} else {
	computerChoice = "scissors";
} console.log("You chose: " + userChoice);
console.log("Computer chose: " + computerChoice);

var compare = function (choice1, choice2) {
    if(choice1 === choice2) 
        return "The result is a tie!";
else if (choice1 === "rock") 
    if (choice2 === "scissors")
    return "Rock crushes scissor -- You WIN";
else 
    return "Paper covers rock -- You lose, better luck next time";
else if (choice1 === "paper")
    if (choice2 === "rock")
    return "Paper covers rock -- You WIN";
else 
    return "Scissors cuts paper -- You lose, better luck next time";
else if (choice1 === "scissors")
    if (choice2 === "paper")
    return "Scissors cuts paper -- You WIN";
else 
    return "Rock crushes scissors -- You lose, better luck next time";

}
compare(userChoice, computerChoice);

#2

Yes there is. Pls see this URL.

Hope this helps! :smile:


#3

Perfect! I think that will work for me. I'll have to give it a shot. Thank you for the reply. :slight_smile:


#4

You're welcome and your kids should have all that fun with the game!

:smile:


#5

Goto can't get messy quickly, why not use a loop or a function? I think it is better practice


#6

You should use goto only in the extreme case - only when its impossible to use the other programming structures to achieve what you want to. Tracking bugs due to goto statements is really hard.

For your problem, the work is easy.

function play(){ // all your RPS code here
    // ...
}

var answer;
do{
    play();

    answer = prompt("Want to play again?");
}while(answer === "Yes");

#7

Thank you all for the input on this topic, it's very much appreciated. :smile: