Rock,Paper, Scissors "What if choice1 is Paper?"


#1


Hi!! I've coded up to this point, but I keep getting a "Syntax Error:Unexpected token else" on the last else if statement. I've reset the code and I've tried deleting the curly brackets a few times but it isn't working, no matter what I do. Can someone please help me on this?!

"Build Paper, Rock, Scissors" game: section 7

Syntax Error: Unexpected Token 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";
    }
}


#2

@andreoli.theresagmai I found a problem here:

and here:

there should be a curly brace({) at the end of those lines.

hope this helps

Steven


#3

Thank you for trying to help. I changed those 2 parts as you mentioned (added curly bracket to the end of each). And the syntax error changed..but there still is one. It now says: "syntax error: unexpected end of input". I really can't tell what is the problem on this now. But thanks again for checking it out.


#4

If anyone has any idea what the problem is, please lemme kno!


#5

I would guess that you need to close your compare function.


#6

Hi @andreoli.theresagmai,

If it may help please see the code sample below.
In summary, there were a few syntax errors on here, the comments below will hopefully help describe what I write here, but @steven.copeland was on the right path here. It was the use of braces that caused the issue. If you make the changes as described below, you can even call the function compare() after to check it out, the rest of the code looks great and it should get you rockin' and rollin' :slight_smile:

```
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")
    { //missing this opening brace    
        if(choice2 === "scissors")
        {
            return "rock wins";
        }
        else 
        {
            return "paper wins";
        }
    }
    else if(choice1 === "paper")
    { //missing this opening brace  
        if(choice2 === "rock"){
            return "paper wins";
        }
        else {
            return "scissors wins";
        }
    }
}//missing closing function brace

compare(userChoice,computerChoice);//let's call the function to test it out

`


#7

Thanks so much for the help! I really appreciate it! Worked like a charm! :grinning:


#8

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