7. What if choice1 is paper? SyntaxError: Unexpected token else


#1

Hello everybody! Can someone please help me to see where is my mistake? I understand, that somewhere and somehow I didn't close the block of "else if" code, so I can't start new "if" block, but can't get where is my mistake.... so, my code is :

var compare = function(choice1, choice2) {
var choice1 = "rock";
if (choice1 === choice2) {
return "The result is a tie!";
} else if (choice2 === "scissors") {
return "rock wins";
} else if (choice2 === "paper") {
return "paper wins";
} else {
return "scissors win";
}
}
else if (choice1 === "paper" && choice1 === choice2) {
return "The result is tie!";
}
if (choice2 === "rock") {
return "paper wins";
} else {
return "scissors wins";
}

And BTW how to post my code like code?


#2

var compare = function(choice1, choice2) {
    var choice1 = "rock";
    if (choice1 === choice2) {
        return "The result is a tie!";
    } else if (choice2 === "scissors") {
        return "rock wins";
    } else if (choice2 === "paper") {
        return "paper wins";
    } else {
        return "scissors win";
    }
} // function ends here
else if (choice1 === "paper" && choice1 === choice2) {
    return "The result is tie!";
}
if (choice2 === "rock") {
    return "paper wins";
} else {
    return "scissors wins";
}

You closed your function too early. Reformat your if statements.

Check this thread to know how to post your code.


#3

Thank you for answer! I deleted my wrong curly bracket and added it at the end of my code to close the function, but I still have the same mistake:

var compare = function (choice1, choice2) {
var choice1 = "rock";
if (choice1 === choice2) {
return "The result is a tie!" ;
}
else if (choice2 === "scissors") {
return "rock wins" ; }
else if (choice2 === "paper") {
return "paper wins" ;}
else {
return "scissors win"; }
else if (choice1==="paper" && choice1===choice2) {
return "The result is tie!";
} if (choice2==="rock") {
return "paper wins";
} else {
return "scissors wins";
}
}


#4
  1. You are overriding choice1 value with rock.
  2. You shouldn't have any else if statements after else statement.
  3. Your checking flow goes like:
    3.1. If computer choice = "rock" then tie.
    3.2. Else if computer choice = "scissors" then rock wins
    3.3. Else if computer choice = "paper" then paper wins
    3.4. Else scissors win (but user choice is always "rock" since you are overriding it).
    3.5. Else if user choice = paper (which is always false, as above) and computer choice = "paper" then tie.
    3.6. If computer choice = "rock" then paper wins, no matter what user chosen (user could choose scissors)
    3.7. Else scissors wins.

To do

  1. Remove line var choice1 = "rock".
  2. First if is OK. Don't change it.
  3. Check if user choice is a "rock"
    3.1. Check if computer choice was "paper"
    3.2. Else (computer choice was scissors, because we excluded choosing "rock" in first if)
  4. Else check if user choice was "paper"
    4.1. Check if computer choice was "rock"
    4.2. Else...
  5. Else...
    5.1. ...
    5.2. ...

#5

Thank you very much for help! Now everything is clear for me and my code works :slight_smile:

var compare = function (choice1, choice2) {
if (choice1 === choice2) {
return "The result is a tie!" ;
} else if (choice1 === "scissors") {
if (choice2 === "paper") {
return "scissors wins" ;
} else {
return "rock wins";
}
} else if (choice1==="paper") {
if(choice2==="scissors") {
return "scissors wins";
} else {
return "paper wins";
}
}
}


#6

Hi arkussss,

I'm also getting stuck on "What if choice1 is paper". Could I get some assistance on what is wrong with my code below?

Thanks!

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

#7

You have 2 closing curly brackets after else if (choice1 === "rock").

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"; // check comment below
        }
    } // remove this curly bracket
} else if (choice1 === "paper") {

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

Also, for better readability, you should use curly brackets even if there's one line statement after else