7/9 Can't understand the issue!


#1

I can't work out where my error is... What am I missing?

error is SyntaxError: Unexpected token else

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


#2

Hi @bitninja99978

Your code should be looks like this

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

compare(); //test your function

Hold on :wink:


#3

Excellent! Thank you for that @onepunch_man


#4

You're code won't work either....

compare() asks for two Strings so you should give it to them.

Another thing, actually a question, why the semicolon's after the 'if'-statement?


#5

I know compare() asks for two strings, it's the reason what I wrote //test your function , he can test it with what he wants.

I learned what it must close variable or condition etc with semicolon because it's like a dot in a sentence, so I do it.
But I'm going to see if it's an obligation or if it's recommend, thanks @betarockstar54455 :wink:


#6

Yeah, conditions and variables always. No offence intended anyway. I was just wondering with the if- statement

example:

if(condtion){
    do stuff
};

The question is why this semicolon is there :wink:
EDIT:
I see what you did there. The semicolon should be behind your statements.

Like so:

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