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


#1

This is really weird. The game works perfectly except it won't accept it as me completing the task because it comes up with this error?

//Variables

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();

//===========================================================================

//Computer Choice selection on whether the computer chooses 'rock', 'paper' or 'scissors'

if (computerChoice < 0.34) 
{
	computerChoice = "rock";
} 
else if(computerChoice <= 0.67) 
{
	computerChoice = "paper";
} 
else 
{
	computerChoice = "scissors";
} 

//===========================================================================

//Compare function on whether the computer wins or the user wins.

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 (choice1 === "rock")
        {
            return "rock wins"
        }
        else
        {
            return "paper wins"
        }
    }


}

//===========================================================================

//Outputting to the console.

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

#2

That was near impossible to read, please format your code in the future? As for the issue the last if statement is wrong, you're checking for choice 1 as scissors and then choice 1 as rock, a typo I assume

else if (choice1 === "scissors")
    {
        if (choice2 === "rock")
        {
            return "rock wins"
        }
        else
        {
            return "paper wins"
        }
    }

#3

Yeh, this is my first time posting a problem on here so it wasn't good to start with but I don't know if you can see now but it should now be formatted?

And I have spotted that type and changed 'choice1' to 'choice2' but it still hasn't changed anything, I still have the same problem :confused:. But thanks for spotting that typo :wink:.


#4

Yeah it looks better now but looking at the same block it should be scissors wins as opposed to paper wins also, looking at the error message you've jumped ahead and it should be returning undefined anyway. Double check what the instructions are and you should be good to go :smile:


#5

Oh there we go! Thank you very much :smile:


#6

Same error message for me. I have scanned through the forum and I have put long time into debugging my code, but still I don't find the error. Here's my code (beware the format, a real newbie on this):

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(choice2 === "scissors"){
return "rock wins";
}
else {
return "scissors wins";
}

if(choice1 === "paper"){
    if(choice2 === "rock"){
    return "paper wins";
    }
    else{
    return "rock wins";
    }
}

if(choice1 === "scissors"){
    if(choice2 === "rock"){
    return "rock wins";
    }
    else{
    return "scissors wins";
    }
}
};

compare(userChoice, computerChoice);

#7

This is how for example the paper case should look like:

else if(choice1 === "paper"){
    if(choice2 === "rock"){
    return "paper wins";
    }
    else{
    return "rock wins";
    }
}

The problem with your code is that the rock case doesn't look anything like this :smile:

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

this is a full if/else if/else statement and each path has a return so nothing after this is reached. You probably saw that there is a problem with this because you changed your else if to if, didn't you? Unfortunately this doesn't solve the problem. Which is that you have only 3 possibilities. Tie, choice2 is scissors -> rock "wins" (what about inputs being scissors and paper?) and the whole rest where scissors wins (always). So fix this rock case according to the example above and you should be fine.


#8

Hey. I took a rematch on the code and now it works. More careful coding and some magic with those {} and now it works. Thanks for quick answer.