This is error - Your code returned 'rock wins' instead of 'scissors wins' when the inputs are paper and scissors


#1

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";
}
if(choice1==="paper") {
}
else if(choice2==="rock") {
return "paper wins";
}
else {
return "scissors wins";
}
}
where should i make change please tell.


#2

Hi @channuu,

just a minor detail please, when posting your code and seeking help please be sure to post the entire code so that other may help you more effectively :smile: with that aside, there are a few errors in your code, below is a working sample you can hopefully use to compare the tow so you may grasp a better understanding.

var userChoice = prompt("Do you choose rock, paper or scissors?").toLowerCase();
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("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 (choice1 === "scissors")
    {
        if (choice2 === "paper")
        {
            return("scissors wins!");
        }
        else
        {
            return("rock wins");
        }
    }
    else
    {
        return("Please check your selection, only enter 'Rock', 'Paper' or 'Scissors");
    }
    
};

console.log(compare(userChoice,computerChoice));
console.log("Computer choose: " + computerChoice);

Some points to note are how the if/else conditions work, you would want to nest them isnide each other so you could control the flow of the code, a quick example

 else if (choice1 === "rock")
 {
         if (choice2 === "scissors")
        {
            return("rock Wins!");            
         }
        else
        {
           return("paper Wins");
       }
 }