Rock, Paper, Scissors additional code


#1


I finished the RPS game. I was attempting to add in some code that would prompt the user to pick a new choice, if they chose something other than rock, paper or scissors. What I have below works, but only once. If I type in a wrong word, it asks me to choose again, but if I type in another wrong word, it uses the second word I typed. How do I make it repeat the process from the start?


{
    var userChoice = prompt("Do you choose rock, paper or scissors?"); {
    if (userChoice === "rock") {
        userChoice = "rock";
    }
    else if (userChoice === "paper") {
        userChoice = "paper";
    }
    else if (userChoice === "scissors") {
        userChoice = "scissors";
    }
    else {
        userChoice = prompt("Choose again");
    }
    }
};
var computerChoice = Math.random();
if (computerChoice < 0.34) {
	computerChoice = "rock";
} else if(computerChoice <= 0.67) {
	computerChoice = "paper";
} else {
	computerChoice = "scissors";
} console.log("Computer: " + computerChoice);
  console.log("Human: " + userChoice);
compare = function(choice1, choice2){

if (choice1 === choice2) {
    return "The result is a tie";
}

    else if(choice1 === "rock") {
    
        if(choice2 === "scissors") {
    return "rock wins";
    }
        else {
        return "paper wins";
    }
}
    else if(choice1 === "paper") {
        
        if(choice2 === "rock") {
            return "paper wins"
            }
        else {
            return "scissors win"
            }
        }
    else if(choice1 === "scissors") {
        
        if(choice2 === "rock") {
            return "rock wins"
        }
        else {
            return "scissors win"
        }
    }    
};
compare(userChoice, computerChoice)


#2

You would want to call the function again from the else block, otherwise it won't run again.

This is one of those "You can lead a horse to water, but you can't make it drink" things


#3

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