Oops, try again. Your code returned 'rock' instead of 'undefined' when the inputs are scissors and paper


#1

Hello i'm trying to solve the game Build "Rock, Paper, Scissors", but at the moment i am stuck with the game.

I didn't change anything of this function. The function literally is:

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

i call the function with the follow way:
compare(userChoice,computerChoice);

The error message is the follow:

Oops, try again. Your code returned 'rock' instead of 'undefined' when the inputs are scissors and paper

the complete 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);

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

#2

I believe it should be choice2. The error said that it shouldn't return anything if input is either paper or scissors but you're returning "rock" when input is scissors.


#3

Can i ask something about the game because i'm not understanding at the last what i'm doing.

if(choice1 === choice2){
        return "The result is a tie!";
    }else if(choice1 === "scissors"){
        
    }else if(choice2 === "rock"){
        return "paper wins";
    }else{
        return "scissors wins";   
    }

I evaluate different options, the computer and me will have rock, paper, scissor. But first thing i check if we have the same element then we have tie. If not i check if i have scissors and i do nothing. After if the computer has rock, it will win. And if i didnt comply, nothing of these, i will have "scissors wins". Can you explain me better why at last scissors will win? Sorry but it is to be sure that i understand well the excercise. Thanks a lot.


#4

I believe you're misreading the instructions here. I think you're on lesson 6. Logically, the way you want to set the game up is to have a computer return rock, paper, or scissors randomly and have the user prompt. Then, for every choice the user can make, if the computer plays a winning choice, computer wins. If player and computer plays same choice, its a tie etc etc.

What they want is to only focus on what if user inputs rock, if it matches the computer's choice, it's a tie. If computer plays paper, it wins. If computer plays scissors, user wins.

if(choice1 === choice2){
        return "The result is a tie!";
    }else if(choice1 === "rock"){
        
    if(choice2 === "foo"){
        return "foo";
    }else{
        return "foo";   
    }
}

#5

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