Syntax error?


#1

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

#2

if(choice1 === choice2) {
        return "The result is a tie";
    }
    else if (choice1===scissors) {
        if (choice2===rock) {
            return "rock wins";
        }}
    else {
        return "scissors wins";
    }

An if statement comes before an else if statement. Also remember to close any curly braces and parentheses you open. Took me a while to realise that myself. For your else if you've only closed one but opened 2.


#3

This part of the code seems to be ok, the semicolon at the end might be a problem (just delete it) but anyway I guess the error comes from the rest of your code.


#4

The if else in your code should be wrapped inside of the else if. Then your would read the two statements as

if choice1 is equal to choice2 -> the result is a tie
if choice1 is scissors and choice2 is rock --> rock wins.
if choice1 is scissors and choice2 is anything else then rock --> scissors wins.

#5

Yes, as @haxor789 mentioned:

if - else if - else statements do not warrant a semicolon after the end bracket. ( ; ) otherwise it looks good to me.

@haxor789, I believe that his logic is sound assuming that he already has an if statement that looks similar to

if(choice1 === choice2) {

in the code above that of which he provided. Or am I wrong?


#6

@betaace64699's logic? No. The problem with this logic is that the else now is on the same level as the if and the else if meaning anything which is not tie or choice1 being scissors is scissors wins.

or @findunn's logic? Yes. This seems to be ok from the logic point of view if the rest of the code fits.


#7

no no, sorry I forgot to specify. I see the logic error for @betaace64699, I thought you meant @findunn's.


#8

So programming is new to me, like really really new. I've tried to understand where I went wrong but I'm drawing a blank. In the simplest language where did I go wrong with my logic, I'd really like to understand. I started programming 2 weeks ago with no prior experience in it.


#9

It's less about programming but about the RPS game logic. What you try to write is this:

if choice1 is equal to choice2 -> the result is a tie!
if choice1 is scissors and choice2 is rock --> rock wins.
if choice1 is scissors and choice2 is anything else then rock --> scissors wins.

What you actually wrote is this:

if choice1 is equal to choice2 -> the result is a tie!
if choice1 is scissors and choice2 is rock --> rock wins.
Any other case -> scissors wins

the problem is that the else should be the default case to

if (choice2===rock) {

but now it is the default case to

else if (choice1===scissors) {

If you want to see an example for this have a look at the hint of 6/9.


#10

I think I finally understand this. If I understand correctly the curly braces opened after else if (choice1===scissors) should be closed after else { return "scissors wins" } and not before


#11

 if(choice1 === choice2) {
        return "The result is a tie";
    }
    else if (choice1===scissors) {
        if (choice2===rock) {
            return "rock wins"
        }
    else {return "scissors wins"}
    }