In the event of a tie


#1

How do I make it so that both the user and the computer are prompted to make another choice for a tie-breaker? Should I create a tiebreaker variable and stick in the compare function when I call it? Below is what I have. I figured I'd keep in simple first, but it's obviously not working. Thanks in advance.

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("Computer: " + computerChoice);

var compare = function(choice1 , choice2) {
if(choice1 === choice2) {
return "The result is a tie, try again!";
var userChoice;
var computerChoice;
}
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 wins"
    }
}
else if (choice1 === "scissors") {
    if(choice2 === "paper") {
        return "scissors wins";
    }
    else {
        return "rock wins"
    }
}

};
compare(userChoice, computerChoice);


#2

@arcwhiz40906 I don't know if this is professional, but I usually just wrap all of the code in a function, called "play," or something, then call it when its a tie. That'll restart the game every time there's a tie. Make sure, if you do this, that you call the function in the first place, otherwise, your code wont run.


#3

Nice I'll try that. Thanks!


#4

This made sense to me but for some reason I'm getting some weird behaviour. For ex. sometimes I get

TypeError: compare is not a function

Here is my code. When it's not wrapped in the play() function it works pretty well.

//wrapper function
var play = function() {


var userChoice = prompt("Do you choose rock, paper or scissors?");

var computerChoice = Math.random();
if (computerChoice < 0.333) {
	computerChoice = "rock";
} else if(computerChoice <= 0.666) {
	computerChoice = "paper";
} else {
    computerChoice = "scissors";
} 

console.log("Computer: " + computerChoice);

if (userChoice != "rock" && userChoice !="paper" && userChoice != "scissors") {
        var userChoice = prompt("What are you funny? Do you choose rock, paper or scissors, wise guy? No caps.");
    }
    else {
var compare = function (choice1, choice2) {
    
    if (choice1 === choice2) {
        return "The result is a tie!";
        play();
        
    }
    else if (choice1 === "rock") {
        if (choice2 === "scissors") {
            return "Rock wins! One point for humans.";
        }
        else {
            return "Paper wins! One point for computers.";
        }
    }
    else if (choice1 === "paper") {
        if (choice2 === "scissors") {
            return "Scissors wins! One point for computers.";
        }
        else {
            return "Paper wins! One point for humans."
        }
    }
    else if (choice1 === "scissors") {
        if (choice2 === "rock") {
            return "Rock wins! One point for computers.";
        }
        else {
            return "Scissors wins! One point for humans.";
        }
    }
}
}

compare(userChoice, computerChoice);

}
play();