javascript 语法错误:意外的标记 Else
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21500439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Syntax Error: Unexpected token Else
提问by user3260811
I am learning Javascript via Codecademy and no have been stumped on this little piece here.
我正在通过 Codecademy 学习 Javascript,没有被这里的这个小部分难住。
I have supposed to write an if else statement.
我应该写一个 if else 语句。
It shows me here in the following that there is a Syntac Error with a missing identifier:
它在下面向我展示了一个缺少标识符的语法错误:
var userAnswer = prompt("Are you feeling lucky, punk?");
if (userAnswer === "yes");
{
console.log("Batman hits you very hard. It's Batman and you're you! Of course Batman wins!");
}
else {
console.log("You did not say yes to feeling lucky. Good choice! You are a winner in the game of not getting beaten up by Batman.");
}
What is wrong with that.... There is no error in this example here:
这有什么问题....在这个例子中没有错误:
if (age < 18)
{
console.log("We take no actions or responsibility. Play at your own risk!");
}
else
{
console.log("Enjoy the game");
}
采纳答案by Niet the Dark Absol
if (userAnswer === "yes");
Remove the semicolon.
去掉分号。
回答by danwellman
There's a semi-colon after the first conditional check. Also, you should always put the opening bracket of the conditional branch on the same line as the brackets
第一次条件检查后有一个分号。此外,您应该始终将条件分支的左括号与括号放在同一行
回答by George
var age;
age = prompt('How old are you?');
if (age < 18)
{
alert("We take no actions or responsibility. Play at your own risk!");
}
else if(age > 18)
{
alert("Enjoy the game");
}