8. What if choice1 is scissors? I don't understand what is wrong with my code! Please help me!


#1

I have been coding this all night and I cannot find where I messed up. My code I know is probably wrong, but I do not know what I did. Hopefully someone can look at this with a fresh pair of eyes and see what I did wrong :

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

#2

The idea is that the compare function first of all only focuses on player 1 which yields 4 cases:

tie
rock
paper
scissors

Ok now that we dealt with player1 what about player2? Well this is done in the rock, paper and scissors case. For example if player1 chooses rock what is left for player2? Rock is covered by the tie case so scissors and paper are left.

so inside your else if (choice1 ==="rock") you need one case for paper and one for scissors (or an else if you trust the user to input only rock paper and scissors).

So if you now have a look at your code:

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

you can see that your else if only inhabits one condition for player2 and even worse it comes after a return "Rock wins"
. As return exits a function this statement is never even reached. And if we assume you deleted the return "Rock wins" you still have the problem that the else is now the alternative to your else if. So if it is not tie or choice1 being rock -> paper wins. Pretty unfair for scissors isn't it? For a working example of a case have a look at the hint of 6/9.