What If Choice1 is Paper


#1

Hey everyone, So I am using this code but it keeps returning ( SyntaxError: Expected ';' )

I've added the ' ; ' at the end of the code, but still receive the error... Any ideas?


#2

Could you copy paste your code the forum? Your function is closed with a } on line 28, that should have a semi-colon. For the rest i would have to run it, which i can't do from a image


#3

Here you go.

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


#4

currently, this is your function:

if (choice1 === choice2) {
        return "The result is a tie!"; 
} else if (choice1 === "rock") {
        if (choice2 === "scissors") {
            return "rock wins.";
        } else {
            return "paper wins.";
        }
}
}  /* this closing curly bracket closes your function */

But your else if choice1 is paper, should also be in the function...


#5

Yeah my coworker was looking at it with me and he pointed out the:

}
}

And I removed the first } and it worked. Thank you for your help.


#7

Could you post a pic of the code that you got working? Please?


#8

Nope, posting answers and asking for answer is against the guidelines, if you need help, post your code


#9

Syntaxerror :confused:

/*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";
}
}


#10

else {
    return "paper wins";
}
}
{ /* what is this curly bracket doing here? */
else if (choice1 === "paper") {

see comment. Also, you need to close function at the end


#11

thank you, sir! Finally!


#13

2 posts were split to a new topic: Can't figure my mistake out