Much needed help "what if choice1 is scissors"


#1

This is the error message I get "Oops, try again.
Your code returned 'scissors wins' instead of 'undefined' when the inputs are scissors and rock" Happens with all possible outcomes.

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


var compare = function(userChoice,computerChoice)
{
    userChoice = choice1;
    computerChoice = choice2;
    
    if(choice1 === choice2)
    {
        return "The result is a tie!";   
    }
    else if(choice1 === "rock")
    {
        if(choice2 === "scissors")
        {
            return "rock wins";
        }
        else
        {
            "paper wins"
        }
    }
    else if(choice1 === "paper")
    {
        if(choice2 === "rock")
        {
            return "paper wins"
        }
        else
        {
            return "scissors wins";
        }
    }
    else if(choice1 === "scissors")
    {
        if(choice2 === "rock")
        {
            return "rock wins";
        }
        else
        {
            return "scissor wins"
        }
    }
console.log(compare(userChoice,computerChoice));
}

#2

why are you using

userChoice = choice1;
computerChoice = choice2;

you dont need that.just take two parameter like

var compare = function(choice1,choice2)

now call the function after closing( } ) function

console.log(compare(userChoice,computerChoice));

check in your first else if you missed return keyword

 else
        {
            "paper wins"
        }

and last statement you wrote scissor wins instead of scissors wins

else
        {
            return "scissor wins"
        }

#3

Thanks for that, the small things we miss up in. Always great having someone else check our work.


#4

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