Build rock paper scissors


#1


Build rock paper scissors


Again, it says I'm doing it right, but I know for a fact something is off because every time I run it it gives me all of the possible answers rather than just the correct one. I'm not sure where I went wrong.


Replace this line with your code. 
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);
function compare(choice1, choice2)
{
if(choice1 === choice2)
{
    console.log("The result is a tie!")
}
else if(choice1 === "rock")
{
    if(choice2 === "scissors")
    console.log("rock wins")
}
    else
    {
    console.log("paper wins")
}
if(choice1 === "paper")
{
    if(choice2 === "rock")
    console.log("paper wins")
}
else
console.log("scissors wins");
if(choice1 === "scissors")
{
    if(choice2 === "rock")
    console.log("rock wins")
}
    else(choice2 === "paper")
    console.log("scissors wins")
}
compare(userChoice, computerChoice)


#2

This time you are getting the function wrong. You need to make compare a variable.

You do this -
function compare(choice1, choice2)

When you should do this-
var compare = function(choice1, choice2)

Use my code below as a example.

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!";
}
    
    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(choice2 === "rock"){
        
        if(choice1 === "scissors"){
            return "rock wins"; } }
            
    else if(choice2 === "paper") {
        
        if(choice1 === "scissors"){
            return "scissors wins";
        }
    }
    }
    
    compare(userChoice, computerChoice)

Kinda dumb how it isn't picking you up on the mistake and instead saying you got it right.

If this was the solution be so kind as to mark my reply as this questions solution, thanks :slight_smile:


#3

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