What if choice1 is scissors?


#1


Oops, try again. There was a problem with your syntax

I get syntax error, also the debugger says "Expected an Identifier instead saw 'else'. Missing semi-colon"(at line 27) I can't see what the problem is with else.


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


#2

return("rock wins");{
            } else{

One bracket doesn't belong there. You've opened and directly closed curly brackets.


#3

thanks, I worked out


#4

Hi @cloudsurfer17495,

You're missing a lot of { } curly brackets for your else if condition, in addition to what @ghostlovescore mentioned, look below

var compare = function(choice1, choice2) {
    if(choice1 === choice2) {
        return "The result is a tie!";
    } else if(choice1 === "rock") { // <===  I add it here here, you missed an opening { for your else if condition to run
    if(choice2 === "scissors"){
        return("rock wins");
        }else{
            return("paper wins");
        }
    }         // <======= I add it here, you're missing a closing } to close your else if.
        else if(choice1 ==="paper")  // <===== same thing, where is your opening {
        if(choice2 === "rock"){
            return("paper wins");
        } else{
            return("scissors wins");
            } else if(choice1 === "scissors")  // <===== where is your closing } for previous else if, and where is your opening { for current else if.....
            if(choice2 === "rock")   // <===== where is the opening } ?
            return("rock wins");{   // <==== this error, ghostlovescore already mentioned
            } else{
                return("scissors wins");
            } else if(choice2 === "paper") // <==== this whole else if is not need anymore....
            return("scissors wins");
{          // <====== even though this is not needed, but just want to mention having { is wrong here.

Use proper indentation so that you can read your code easily next time.

Format like:

var compare = function(choice1, choice2) {
    if(choice1 === choice2) {
        return "The result is a tie!";
    } else if(choice1 === "rock")  // <==== missing the opening {
        if(choice2 === "scissors"){
            return("rock wins");
        } else {
            return("paper wins");
        }                 
     // <==== missing the closing } for else if
    else if(choice1 ==="paper")  // <==== missing the opening {
        if(choice2 === "rock"){
            return("paper wins");
        } else {
            return("scissors wins");
        }           
       // <==== missing the closing } for else if
    else if(choice1 === "scissors")  // <==== missing the opening {
        if(choice2 === "rock")  // <===== missing the the opening {
            return("rock wins");{
        } else {
     .......................
     ...............
    .......

The indentation will make it easier for you to spot the mistakes/errors in your code when you reexamine your code.

Hope this helps. :slight_smile:


#5

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