Hi I have this code right now
var pare = function (choice1, choice2)
{
if (choice1 === choice2)
{
return "The result is a tie!";
}
};
if (choice1 === "Rock")
{
if (choice2 === "Scissors")
{
return "Rock wins!";
}
else (choice2 = "Paper")
{
return "Paper wins!";
}
}
but I keep getting a Synatax error, illegal return statement.
I don't understand why I am getting this error, am I doing something wrong? I believe the syntax is all correct.
I am using an online editor by the way, not an actual IDE.
Hi I have this code right now
var pare = function (choice1, choice2)
{
if (choice1 === choice2)
{
return "The result is a tie!";
}
};
if (choice1 === "Rock")
{
if (choice2 === "Scissors")
{
return "Rock wins!";
}
else (choice2 = "Paper")
{
return "Paper wins!";
}
}
but I keep getting a Synatax error, illegal return statement.
I don't understand why I am getting this error, am I doing something wrong? I believe the syntax is all correct.
I am using an online editor by the way, not an actual IDE.
choice2 = "Paper"
looks like a typo
– Bathsheba
Commented
Dec 18, 2013 at 8:05
else if(choice2 == "Paper")
? - and wrap the if inside the pare
function :)
– Niccolò Campolungo
Commented
Dec 18, 2013 at 8:06
I think you want to write the following
var pare = function (choice1, choice2)
{
if (choice1 === choice2)
{
return "The result is a tie!";
}
if (choice1 === "Rock")
{
if (choice2 === "Scissors")
{
return "Rock wins!";
}
else if (choice2 === "Paper")
{
return "Paper wins!";
}
}
};
return statement is only valid inside the function.
Look at the code
var pare = function (choice1, choice2)
{
if (choice1 === choice2)
{
return "The result is a tie!";
}
};
this part of the code will work fine. but after this semicolon (;)
a separate part of the code is started which eventually is not the part of function as you function ended with the semicolon, that's why lower part of the code is giving this error.
Second thing, Your else statement makes no sense. You code should be like this
var pare = function (choice1, choice2) {
if (choice1 === choice2) {
return "The result is a tie!";
}
if (choice1 === "Rock") {
if (choice2 === "Scissors") {
return "Rock wins!";
} else if (choice2 === "Paper") {
return "Paper wins!";
}
}
};
Also, I made this mistake, when using debugger;
or line break somewhere, don't return in your browser's console. This returns the same syntax error because your return is not wrapped in a function.
In console don't:
return choice1 === choice2
// error
Instead just do:
choice1 === choice2
// true or false