7/9 SyntaxError: unexpected token else


#1

It says unexpected token else but I feel as though my i have closed everything correctly and i used the else statement correctly. Any help would be appreciated.


#2

Hi @siddrajpoot,

If this is not all the code could please go ahead and post that, sometimes the written code is simpler to work with when asking for help rather than an image :), however that said you error appears to be on line 30, you have an semi-colon at the end of your IF statement,

if (choice2 === "rock); //remove semi-colon

you need to drop this and hopefully that should resolve your issue. If not please do post all the code you are having problems with and someone one will be sure to help as soon as they can.


#3

wow im an idiot.... the rest of the code is just comments so it doesnt have any value. Thanks so much!!


#4

lol, think of it more as being "in training", that's my story and I'm sticking to it :wink:


#5

Hi, I also have an SyntaxError: expected expression, got keyword 'else' Could someone help me?
Here's the javascript:

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

} else if(choice1 === "paper") {
if (choice2 === "rock")
return "paper wins!";
} else { 
return "scissors wins";
}
};

The first part in between the /* & */ is a comment so it shouldn't affect the outcome. I also don't how to nest things nicely so sorry for the mess :wink:. Oh, I am on lesson What if choice1 is "paper"; 7/9
Thank you


#6

you are missing a { for 2 of your if statements and a } at the end of your else if section


#7

You are right. Thanks a ton! :stuck_out_tongue_winking_eye:


#8

WHAT'S WRONG WITH MY SYNTAX NOW!?!?!?!?!?!?!?!?!? i'm on lesson 8/9---maybe this isn't the best spot to ask a question on because i'm actually on lesson 8/9 :stuck_out_tongue_winking_eye:

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

Thank you


#9

no opening { on the if statement :smile: it should be

else if(choice1 === "scissors") {
    if (choice2 === "rock") { /// This line opens the if
        return "rock wins";
    } else { ///closes if and opens else
        return "scissors wins";
    } ///closes else
} ///closes else if
}; ///closes function

#10

Hey, it's working but I forgot what calling the function is anymore. To be more precise, I just forgot how to to it!
???
:stuck_out_tongue_closed_eyes:
Thank you


#11

make the function run by using functionName(). In this case compare(userChoice,computerChoice);


#12

Awesome, but do I put it in like this:

} else if(choice1 === "scissors") {
if (choice2 === "rock") {
return "rock wins!";
} else {
return "scissors wins";
};                                                              
compare(userChoice,computerChoice);

or like this?:

} else if(choice1 === "scissors") {
if (choice2 === "rock") {
return "rock wins!";
} else {
return "scissors wins";
compare(userChoice,computerChoice);
};

Well, I shouldn't put it like that either way because thay are both wrong. :rage:
Help!!!!!!!!!!!!?


#13

You are still missing the } to close the function:

and you'll want to call your function from OUTSIDE like you did in the above example


#14

Like this?---but I am still wrong. :rage: What have I not understood!?!?!?!?!?!?!?!?!?!?!?!?!?!?!?!? Oh my gosh, I must be driving you insane!

} else {
return "scissors wins";
}
};
compare(userChoice,computerChoice);

#15

Whoops sorry! You're missing 2:


#16

It worked! Thanks a million! :grinning: :relaxed:


#17

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"

        }
    }
}
}

#18

Can you please tell mewhere am I going wrong?

Need some techniques to close the nested if elses...suggestions are appreciated.


#19
else if (choice1 === "paper") {

    if (choice2 === "rock") {

        return "paper wins"

        else

here you're missing to close the if with a } after the return part so now the else is part of your if which is technically impossible. Have a look at your first case to see how to match the {}