Lesson 2 part 8 "Oops, try again. Your code returned 'rock wins' instead of 'undefined' when the inputs are scissors and paper" Please help!


#1


https://www.codecademy.com/courses/javascript-beginner-en-Bthev-mskY8/1/4?curriculum_id=506324b3a7dffd00020bf661

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

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

Can't seem to figure out why it's giving me this error message. Probably something wrong with my bracketing format but not sure.

#2

I updated my code and put colons after //return "rock wins"// to make //return "rock wins";// and for paper wins as well. But another odd thing about it is the return box on the right will reply "scissors wins" and still tell me it's telling me that its replying "rock wins". Any explanation?


#3

Try removing the choice conditions (choice === "rock/paper/scissors wins") from all of your else code blocks.

for example:
else { (choice2 = "paper")
return "paper wins"

Remove the italicized part


#4

@biff77

I did as you said. didn't even realize that the = is for text (or strings) and = is for numbers . I changed each === to = in my code and now it says this:

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


#5

Waaaaaait a minute. I was just talking about your else code blocks. All your (choice1 === "rock/paper/scissors wins") conditions should still use the === equality operator. You start using = in your conditions from else if (choice1 = "scissors") up to the end of your compare function. I would use the === operator.


#6

Ah I see what you did there, I undid the initial change i made then did as you said and got this -

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

}
};
compare(userChoice, computerChoice)

But now its saying
Oops, try again. Your code returned 'paper wins' instead of 'undefined' when the inputs are scissors and paper


#7

Look at this:

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


#8

Woops, silly mistake thanks for pointing that out. The program accepted my answer. I appreciate your time and help biff75!


#9

Happy to help :grin:


#10

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