Rock paper scissors issue


#1

I am a grandma to 2 little ones. Mother to 5. I am trying to learn coding to help my younger grandchildren and my last two boys at home to learn to code.

I am not understanding these if/else statements well enough.

Here is my code:
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";
            }
            if (choice1 === "paper"){
                else if (choice2 === "rock" {
                    return "paper wins";
                    }
                    else {
                        return "scissors wins";
                        }

}
};

What am I missing?


#2

your if and if else statements are not nested properly.

i would suggest to start rebuilding this logic, start with the first set of if statements, using the choice1 variable.

if (condition) {
    block of code
} else { 
    block of code
}

if (choice1 === "rock") { will run if true } else { will run if false };
if (choice1 === "paper") {true} else {false};
if (choice1 === "scissors") {true} else {false};

you can have if (choice1 === "scissors") {} or have this in an else {}.

then start filling in the codes between the {} brackets, another if, else statements, but this time using the choice2 variable


#3

Basically your second "if" statement should be an "else if" and should be in line with your first "else if" kinda like this

if (choice1 === choice2) {
    block of code;
}
else if (choice1 === "rock"){
    if (condition){
        block of code;
    }
    else{
        block of code;
    }
}
else if (condition){
    if (condition){
        block of code;
    }
    else{
        block of code;
    }
}

hope this is helpful


#4

So I have a small issue as well with the same task.
This is my code:

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

It's giving me everyones favorite error "SyntaxError: expected expression, got keyword 'else'"
Maybe someone could help me out? - I'm kind of new at JS but really interested in learning.

Any help appreciated!


#5

@eto666 your error is as it says "Syntax error" check your first if statement block, can you see the mistake ?

if (choice1 === choice2) {
     return("The result is a tie!")
 };

#6

@cssblaster73027, I don't know if I'm being a dummy but honestly, I can not see the error. :octopus:
Could it perhaps be the semicolon?
- It was! thanks, mate!


#7

According to what you wrote you are also missing a { from the if on your else if statement and one for the end of the else if


#8

Hi Debbi,

I see that you have long since solved this problem, but I want you to know that I think that what you're doing for your grandchildren is very admirable and I wish I had a grandmother like you.

If you ever need help with anything just message me and I'd be happy to help - I never take more than a few hours to reply.

  • Chris