I am getting an error that says "SyntaxError: Unexpected token else


#1

Can someone please tell me what I am doing wrong?
I am getting an error that says "SyntaxError: Unexpected token else
".
I thought I followed the instructions.
Thank you in advance.


7. Unexpected token else
#2

In your first else-if statement your if has no opening bracket.


#3

Thank you. I am looking through it. Made a whole bunch of adjustment and got myself confused in the logic.
I think I've got the first section right.
Now when it comes to the paper choice. I am confused because it does not give me a return value to in put according to the else if formula.
Please view below.

the error given is: "SyntaxError: Unexpected end of input

//First section that I think I got right
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";
}
//What if choice1 is paper starts here below
if (choice1==="paper"){ //No retuen value on this logic
if (choice2==="rock"){
return "paper wins";
} else {
return "scissors wins";
}

};


#4

When choice1 is "paper", you check if choice2 is equal to rock. If so, paper wins. In all other cases scissors will win. That's why you use the else. Because there is an else there will always be a return value.

To clarify:

if (condition) {
    if (condition2) {
        return 1;
    } else {
        return 1;
    }
}

Is the same as:

if (condition) {
    if (condition2) {
        return 1;
    } 
    return 1;
}

I hope I've been clear because my head is a bit of a chaos a the moment :grin:.


And it would be nice if you could format your code like this:

 ```
 Your code
 ```

#5

thank you. I will look through it again. and implement the format you suggested.


#6

I am so confused. LOL


#7

If you're having a hard time writing code, try thinking up some pseudo code first.
Like:

IF choice one is paper
    IF choice two is rock
       THEN paper wins
    ELSE 
      THEN scissors wins

#8

Thanks @dutchc0de it finally went through. I have to get used to the punctuation. Mathematically speaking it is just the application of a formula. it's just mind bending. but I recon I will adjust with practice.
Nevertheless don't go anywhere I might need you again:grin:.


#9

I am having trouble with the last part.
Error code SyntaxError: Unexpected token }

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

//declaring the compare function
var compare = function(userChoice,choice2){
if (userChoice===computerChoice){
return "The result is a tie!";}

//if userChoice is rock
else if(userChoice==="rock") {
if(computerChoice==="scissors") {
return "rock wins";}
else {
return "paper wins";}
}

//What if userChoice is paper
else if (userChoice==="paper") {
if (computerChoice==="rock") {
return "paper wins";}
else {
return "scissors wins";}

}

//What if userChoice is scissors
else if(userChoice==="scissors") {
if (computerChoice==="rock") {
return "rock wins";}
else {
return "scissors win";}

}

}
};

//Calling the functions
console.log("You have chosen" + " " + userChoice);
console.log("The computer has chosen" + " " + computerChoice);
console.log(compare (userChoice, computerChoice);


#10

Since you're getting the error:

SyntaxError: Unexpected token }

You know you're looking for an closing bracket that is out of place, or in some cases a missing opening bracket.

Let's analyse the last part of your function and match all opening brackets with their closing counterparts:

//declaring the compare function
var compare = function(userChoice,choice2){                    // 1
    if (userChoice===computerChoice){                          // 2
        return "The result is a tie!";}                        // 2

    /* Other else if statements omitted */

    //What if userChoice is scissors
    else if(userChoice==="scissors") {                         // 3
        if (computerChoice==="rock") {                         // 4
            return "rock wins";}                               // 4
        else {                                                 // 5
            return "scissors win";}                            // 5

    }                                                          // 3
    }                                                          // 6?
};                                                             // 1

We've given every opening and closing bracket a number, as in opening bracket 1 matches closing bracket 1 and etc. Notice how closing bracket 6 did not get matched up with an opening bracket? This means that there must be an opening bracket missing, or, more likely, closing bracket 6 is unnecessary.


Read this last:

When you've dealt with the SyntaxError of the closing bracket, you will run in to another syntax error. Take a look at the last line of your code:

console.log(compare (userChoice, computerChoice);

#11

Thank you @dutchc0de for the clarification. That is exactly what the problem was. And concerning the last line last line I put a closing parenthesis.
Now I think there is a problem in the logic as I am getting this error:
"Oops, try again. Your code returned 'scissors win' instead of 'undefined' when the inputs are scissors and rock"
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";}

//declaring the compare function
var compare = function(userChoice,choice2){
if (userChoice===computerChoice){
return "The result is a tie!";}

//if userChoice is rock
else if(userChoice==="rock") {
if(computerChoice==="scissors") {
return "rock wins";}
else {
return "paper wins";}
}

//What if userChoice is paper
else if (userChoice==="paper") {
if (computerChoice==="rock") {
return "paper wins";}
else {
return "scissors wins";}

}

//What if userChoice is scissors
else if(userChoice==="scissors") {
if (computerChoice==="rock") {
return "rock wins";}
else {
return "scissors win";}

}

};

//Calling the functions
console.log("You have chosen" + " " + userChoice);
console.log("The computer has chosen" + " " + computerChoice);
console.log(compare (userChoice, computerChoice));


#12

nvm @dutchc0de. It went through. Thanks again.


#13

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