[英]Syntax Error: Unexpected token Else
我正在通过 Codecademy 学习 Javascript,这里没有人被这个小作品难倒。
我应该写一个 if else 语句。
它在下面向我展示了缺少标识符的语法错误:
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.");
}
那有什么问题......这里的这个例子没有错误:
if (age < 18)
{
console.log("We take no actions or responsibility. Play at your own risk!");
}
else
{
console.log("Enjoy the game");
}
第一次条件检查后有分号。 另外,您应始终将条件分支的开头括号与括号放在同一行
if (userAnswer === "yes");
删除分号。
去掉后面的分号
if (userAnswer === "是");
如果你把分号放在那里,你是在告诉脚本停在那里而不是渲染下一个条件语句“else”[SyntaxError: Unexpected token else]
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");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.