8. What is choice1 is scissors? Please help!


#1

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


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

What have I done wrong?


#2

It shouldn't return undefined but it also shouldn't return 'paper wins' - what are the inputs that it says were used and what should the result be?


#3

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

Those are the only parts where it would have caused a problem, yet again I have no clue why it would return undefined..


#4

It doesn't and it shouldn't. Consider the input and ask yourself what the result should be. Then test it and see if your code gets it right.


#5


#6

Those are pics before the lesson and after it, when i renew the code to the code above, it has the same effect. I'm puzzled.


#7

Sorry you're not saying anything that I have a reply for. Try explaining why you're puzzled or asking direct questions.

(It's up to you to make it clear what you want to know, to explain what's preventing you from going forward)


#8

Your response is not very helpful, OP has provided you with lots of information as to why he is puzzled. I have the exact same problem, and I believe it is a bug of some sort unless someone else can confirm it isn't. OP is puzzled because this exercise appears to be not working properly or is not returning helpful information regarding what it is looking for.

I have code similar to OPs--it resolves in every other exercise perfectly fine, but as you can see by the exercise, it is ASKING to have "undefined" returned, which does not make any sense? Why would the exercise want you to return an 'undefined' answer?

It does not matter what I change in the code, it always produces this error asking for an "undefined" answer. My code appears to return normal answers in the console otherwise.

Here is a screenshot of the same issue for reference:


#9

I rewrote my code to explicitly match the exercise and it worked properly (I possibly had a typo in the original).

It seems this exercise is confusing because the "undefined" answer doesn't really specify exactly what it is looking for.

I can see by your code that you have an additional semi-colon after your closing bracket in your "if (choice1 === choice2)" tree. Try removing that to see if it helps?

I have a screenshot of the successful code here http://i.imgur.com/vx9Cw0I.png


#10

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