What if choice 1 is scissors help


#1

Please help me with this, receiving syntax error saying "unexpected token else". Is it a curly bracket again??? It is frustrating to spend hours only to find out its a bracket or something as similarly simple... It seems like the margin for error should be a bit more forgiving or you should be able to move on from a problem and have the program just tell you your simple mistake. I have been working on this Rock Paper Scissors game for three days now, trying to figure out the mistakes on my own.

~~~~
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!";
}

console.log(compare (userChoice,computerChoice));   

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";
}
}
}
};
console.log(compare (userChoice, computerChoice))
~~~


#2

Hi this is my code and its works

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

compare(userChoice, computerChoice);

#3

Do you now that grammar nazi example of

Let's eat grandpa! vs Let's eat, grandpa!

Well It's kind of similar when using programming languages. The mistakes might seem little but the implication might really get you in trouble.

About getting the brackets right, it really helps if you format your code. One level of indentation whenever you enter a block of code {} and such things:

This is just your code used jsbeautifier.org (cause of laziness) and the forum tools (three backticks before and after the code) to make it look like code:

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!";
    }

    console.log(compare(userChoice, computerChoice));

    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";
    }
}
}
};
console.log(compare(userChoice, computerChoice))

As you can now see more easily the last else if case is not inside of your function.