What if choice1 is scissors?


#1

Can someone please help me figure out what is wrong? Every time I try to submit my code I receive an error message that says "SyntaxError: expected expression, got '}'"

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 === "paper") {
        return "scissors wins";
    }    
    else {    
        return "rock wins";
    }}

};

compare(userChoice, computerChoice);
};


#2

Stray curly brace. Remove that.


#4

Totally wrong 

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

If you do like you say aou get

SyntaxError: Unexpected token else


#6

Thanks for the heads up. I took them out but am still not getting the correct code


#7

what do you mean? the last sentence under the commented code is not making sense to me.


#8

also what do you need the very last curly bracket for?

(here:)


#9

removed that. I guess I didn't need it. I have been staring at this for so long lol


#10

:grin: :thumbsup: Is there any other problem?


#11

There must be something wrong still, it's coming through with the "SyntaxError: expected expression, got keyword 'else'" when I try to submit my code. Do you have any other suggestions?


#12

What does your code look like now? Maybe I can find out what is wrong then


#13

My code now looks like:

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 === "paper") {
        return "scissors wins";
    }    
    else {    
        return "rock wins";
    }

};

compare(userChoice, computerChoice);


#14

I found out the answer:

copy and paste your old code back into your lesson only changing this:

you need the double curly brackets the only thing you do not need is the very last curly bracket

like @mtf said.

your code should look like this now

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 === "paper") {
        return "scissors wins";
    }    
    else {    
        return "rock wins";
    }}
};

compare(userChoice, computerChoice);

#15

awesome! It finally worked! Thank you so much :slight_smile:


#16

:+1: :+1: :+1: :+1: :+1: good


#18

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