How do I make Rock,Paper,Scissors restart and ask for userChoice again if Tie or Invalid Entry?

games
game

#1


Rock,Paper,Scissors, Part 9
https://www.codecademy.com/en/courses/javascript-beginner-en-Bthev-mskY8/1/5?curriculum_id=506324b3a7dffd00020bf661


No Errors


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";
} 

var compare = function(choice1, choice2) {
    if(choice1 === choice2) {
        return "It was a Tie!";
    }
    else if(choice1 === "rock") {
        if(choice2 === "scissors") {
            console.log("Computer: " + computerChoice)
            return "rock wins";
        }
        else {
            console.log("Computer: " + computerChoice)
            return "paper wins";
        }
    }
    else if(choice1 === "paper") {
        if(choice2 === "rock") {
            console.log("Computer: " + computerChoice)
            return "paper wins";
        }
        else {
            console.log("Computer: " + computerChoice)
            return "scissors wins";
        }
    }
    else if(choice1 === "scissors") {
        if(choice2 === "rock") {
            console.log("Computer: " + computerChoice)
            return "rock wins";
        }
        else {
            console.log("Computer: " + computerChoice)
            return "scissors wins";
        }
    }
    else {
        return "Your choice is invalid"
    }
}
compare(userChoice,computerChoice)


#2

Hello @coursesurfer14381,

just call the function again in your tie and invalid prompt branches of your if...else statement, Like so,

var rand_func = function(a_prompt) {
    if a_prompt == valid {
        /*do something*/
    } else 
        var input = prompt("What you entered was invalid, Try again");
        rand_func(input);
    }
};

Then, it will be able to constantly be in a loop unless the prompt is valid...


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.