Unexpected token else, could someone help me? Rock, paper, scissors game


#1


Hello, I am currently trying to make the rock, paper scissors game. But SyntaxError: Unexpected token else pops up when I try to run the game. I have tried to run the first part (before the function) and that part runs as it normal.
Could someone pleas help me find the error?

Here is the link that I copied from the exercise:

https://www.codecademy.com/courses/javascript-beginner-en-Bthev-mskY8/1/4?curriculum_id=506324b3a7dffd00020bf661#


When I try to run the code I get "SyntaxError: Unexpected token else".

Here is my 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);

function compare(choice1 , choice2){
    if (choice1 === choice2){
        return "The result is a tie!"}; //end of 1. if-statment
    else if(choice1 === "rock"){
         if (choice2 === "scissors") {return "Rock wins"}; 
        else {return "Paper wins"}; 
        }; //end of 2 statment
    else if (choice1 === "paper"){
        if (choice2 === "rock") {return "Paper wins"}; 
        else {return "Scissors win"}; 
        }; // end of 3 statment
    else (choice1 === "scissors"){
        if (choice2 === "rock"){return "rock wins"}
        else {return "Scissors wins" };
        };//end of 4 statment
    
    }; //compare function
    
    compare(userChoice, computerChoice)


Thank you in advance!


#2

so one thing I'm noticing is your returns have incorrect syntax.
its supposed to be

return "some random text";

and you did this right in some places but in others you tried closing an if statement and it looks like this

return "Scissors win"};


#3

I think you meant for this to be an else if statement because else statements don't take parameters.


#4

}else if(choice1 === "scissors"){
if (choice2 === "paper"){
return "scissors wins";
}else {
return "rock wins";
}
}
I changed the last one because it was just a copy of the first one. I got it working but I might need a couple of minutes to figure out what you need to change in yours other than the returns and this last part.


#5

Thank you, I have changed to:
else if (choice1 === "scissors"){
if (choice2 === "paper"){return "Scissors wins";}
else {return "Rock wins" ;}
}
}
I still get SyntaxError: Unexpected token else. But you are probably right about me having to change it to else if
Is the return "Scissors wins";} part correct now?
Thank you for your help


#6

ya you fixed it. I just have to find the last thing that I fixed really quick


#7

you need a ; at the end of this. I'm not sure if it will stop the program from running.

if you took care of all of the switched ; } than it should run. if not let me know what the error says and what your current code is.


#8

Thanks, it is still not running, I still get "unexpected token else"
My code now is:

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

function compare(choice1 , choice2){
if (choice1 === choice2){
return "The result is a tie!";} //end of 1. if-statment
else if(choice1 === "rock"){
if (choice2 === "scissors") {return "Rock wins";}
else {return "Paper wins";}
}; //end of 2 statment
else if (choice1 === "paper"){
if (choice2 === "rock") {return "Paper wins";}
else {return "Scissors win";}
}; // end of 3 statment
else if (choice1 === "scissors"){
if (choice2 === "paper"){return "Scissors wins";}
else {return "Rock wins" ;}
}//end of 4 statment

} //compare function

compare(userChoice, computerChoice);

Thanks again.


#9

delete the ; where I have <---------

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);
function compare(choice1 , choice2){
if (choice1 === choice2){
return "The result is a tie!";} //end of 1. if-statment
else if(choice1 === "rock"){
if (choice2 === "scissors") {return "Rock wins";}
else {return "Paper wins";}
};<------- //end of 2 statment
else if (choice1 === "paper"){
if (choice2 === "rock") {return "Paper wins";}
else {return "Scissors win";}
};<------ // end of 3 statment
else if (choice1 === "scissors"){
if (choice2 === "paper"){return "Scissors wins";}
else {return "Rock wins" ;}
}//end of 4 statment
} //compare function

compare(userChoice, computerChoice);

I think that should work. if not I might have missed one of the semicolons but you can probably find where it is.


#10

THANK YOU so much. It finally works now.


#11

no problem, anytime :panda_face:


#12

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