7/9 Help me please


#1

I don't know where's my error..

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

#2

Here is missing one "(" after "if":


#3

in addition to what @julietaatanasova already mentioned you seem to miss a } for the else if before the else. Maybe have a look at the hint of 6/9. Also why do you catch all other cases with else scissors wins?


#4

Thanks @julietaatanasova and @haxor789


#5

Me again... i`ve read the post you asked me to look at and i've already asimilated that concept of the syntax. I must acept that my error most of the times is that a do not take care perfectly of the syntax of the code.

Now, e.g, i've watched carefully, modified some syntaxis but still i cannot figure out the solution..

Here's the code:

var comparar = function(eleccion1, eleccion2) {

if (eleccion1 === eleccion2) {
return "¡Es un empate!";
}

else if (eleccion1 = "piedra") {
    if (eleccion2 = "tijera") {
        return "piedra gana";
        }
}
        else {
            return "papel gana";
        }
else if (eleccion1 === "papel") {
    if (eleccion2 === "piedra") {
        return "gana papel";}
        else {
            return "gana piedra";
            }
}
};

"Error: SyntaxError: Unexpected token else"


#6

@haxor789, heres the properly formatted version:

var comparar = function(eleccion1, eleccion2) {
    if (eleccion1 === eleccion2) {
        return "¡Es un empate!";
    }
    else if (eleccion1 = "piedra") {
        if (eleccion2 = "tijera") {
            return "piedra gana";
        }
    }
        else {
            return "papel gana";
        }
    else if (eleccion1 === "papel") {
        if (eleccion2 === "piedra") {
            return "gana papel";
        }
        else {
            return "gana piedra";
        }
    }
};

It's not corrected in any way other than formatting. I spotted one mistake at least, but you can take this one.


#7

i see the same structure :confused:


#8

I just put the code into one thing rather than multiple quote boxes, which is what your post looks like. Mine also has syntax highlighting.


#9

    else if (eleccion1 = "piedra") {
        if (eleccion2 = "tijera") {
            return "piedra gana";
        }
    }
        else {
            return "papel gana";
        }

if you have a look at matching {} you can see that the else if closes after the if. So the else which should be an alternative for eleccion2 == "tijera" is now an alternative for else if (eleccion1 = "piedra") {. Now the problem is that the else finishes the statement and therefore the upcoming else if raises an error. For a working example see the next case that starts at else if (eleccion1 === "papel") {. You might also have recognized that I wrote == instead of = . The reason for this is that = is an assignment operator while == is a comparison operator. So what happens when you use = is that you make for example eleccion2 become "tijera" instead of comparing it to this value. What is not taught here explicitly but which happens next is that the statement eleccion2 = "tijera" has a value of "tijera" and as you can read here http://www.sitepoint.com/javascript-truthy-falsy/ non empty strings are treated as truthy. So this statement is always chosen even if it is not true.

@jibblyj Thanks for the formatting but you can really answer the questions yourself if you find the mistakes. Always glad when there is competent support in answering questions and if you missed something I might add it if I see it.


#10

Thanks @haxor789 . I've been reading and reading your answer and i couldn't see my error in your explanation. I finally get it! Sometimes i have more difficult by reading and understanding in english than the 'logic' problem. Sorry if i bother too much jaja.

**And the thing about '=' and '==' its because i do, or try to do fast the excercise and forgot some symbols.


#11

I'm completely stuck on this one too, keep getting the error message, "there was a problem with your Syntax, syntax error: unexpected token Else".
This 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";
} else if(choice1 === "paper") {
if(choice2 === "rock") {
return "paper wins";
} else {
return "scissors wins";
}
}
};


#12

@nataliemp

You're missing a closing curly bracket } right after your first return "paper wins"; on line 8.

I added the curly bracket in the following code on line 9, so it should work properly now.

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

#13

Thanks so much! Working fine now, it certainly is the little things!


#14

No problem, glad I could help