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


#3

There's something wrong with your else if cases maybe have a look at the hint of 6/9 to see a working example. And maybe start again from there again.

This is (part of) your code no changes made just formatting issues:

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

As you can see you wrapped everything in one else if. Even worse this whole code of the else if is pointless as most of it comes after a return and as return exits the function it is never even possible to reach this. Also it returns rock wins in case that the user chooses rock. So unless your a TRVE Metalhead this is probably not how this game works :wink:

Hope this helps if there are any questions feel free to ask.