Oops, try again. Your code returned 'The result is a tie!' instead of 'undefined' when the inputs are scissors and rock


#1

  1. What if choice1 is scissors?

Hi, I'm facing this error:
Oops, try again. Your code returned 'The result is a tie!' instead of 'undefined' when the inputs are scissors and rock. Could anyone help me please?


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

}


#2

On this part:

You wrote if(userChoice=computerChoice) instead of if(userChoice==computerChoice)

Remember:
= assign values
== compares values


#3

Hi gracie017,

Thanks for your response.

I tried it. And I got another error: Oops, try again. Make sure to call your compare function with userChoice and computerChoice as the arguments!

Any idea of what it could be?


#4

Can you post your revised code?
Anyway, i think this is the problem: You need to call your compare function and pass in userChoice and computerChoice as your two arguments. and use console.log to display the data.


#5

Sure!

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(userChoice,computerChoice){
if(userChoice===computerChoice){
return "The result is a tie!";
}

else if(userChoice==="rock"){
        if(computerChoice==="scissors"){
            return "rock wins";
        }
        else{
            return "paper wins";
        }
}

else if(userChoice==="paper"){
        if(computerChoice==="rock"){
            return "paper wins";
        }
        else{
            return "scissors wins";
        }
}

else if(userChoice==="scissors"){
        if(computerChoice==="rock"){
            return "rock wins";
        }
        else{
            return "scissors wins";
        }
}

}


I'm getting this: Oops, try again. Make sure to call your compare function with userChoice and computerChoice as the arguments!


#6

What I found out was when I was getting the same thing, I was missing the ending else in the if/else statement. All the nested if/else statements are good. Just gotta make sure the outer if/else statements are complete. So look at your code, you have the if , else if , else if, else if. but you're missing the last else. Hope that helps.