admin管理员组文章数量:1026989
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.
Share edited Dec 18, 2013 at 8:49 웃웃웃웃웃 12k15 gold badges62 silver badges94 bronze badges asked Dec 18, 2013 at 8:03 Ankur JainAnkur Jain 151 gold badge2 silver badges8 bronze badges 4-
1
choice2 = "Paper"
looks like a typo – Bathsheba Commented Dec 18, 2013 at 8:05 -
2
Maybe you wanted
else if(choice2 == "Paper")
? - and wrap the if inside thepare
function :) – Niccolò Campolungo Commented Dec 18, 2013 at 8:06 - Go cheat on your classmate : stackoverflow./q/20622970/1636522 :D – user1636522 Commented Dec 18, 2013 at 8:08
- Thanks LightStyle, that did it! – Ankur Jain Commented Dec 18, 2013 at 8:46
3 Answers
Reset to default 2I 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
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.
Share edited Dec 18, 2013 at 8:49 웃웃웃웃웃 12k15 gold badges62 silver badges94 bronze badges asked Dec 18, 2013 at 8:03 Ankur JainAnkur Jain 151 gold badge2 silver badges8 bronze badges 4-
1
choice2 = "Paper"
looks like a typo – Bathsheba Commented Dec 18, 2013 at 8:05 -
2
Maybe you wanted
else if(choice2 == "Paper")
? - and wrap the if inside thepare
function :) – Niccolò Campolungo Commented Dec 18, 2013 at 8:06 - Go cheat on your classmate : stackoverflow./q/20622970/1636522 :D – user1636522 Commented Dec 18, 2013 at 8:08
- Thanks LightStyle, that did it! – Ankur Jain Commented Dec 18, 2013 at 8:46
3 Answers
Reset to default 2I 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
本文标签: javascriptSyntax errorIllegal return statementStack Overflow
版权声明:本文标题:javascript - Syntax Error, Illegal return statement - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1744908916a2123492.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论