Javascript 语法错误:意外标记Else
我正在通过Codecademy学习Javascript,没有人在这篇小文章中被难倒 我应该写一个if-else语句 以下内容显示了缺少标识符的Syntac错误:Javascript 语法错误:意外标记Else,javascript,identifier,Javascript,Identifier,我正在通过Codecademy学习Javascript,没有人在这篇小文章中被难倒 我应该写一个if-else语句 以下内容显示了缺少标识符的Syntac错误: 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
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(年龄<18岁)
{
log(“我们不采取任何行动或承担任何责任。玩游戏的风险由您自己承担!”);
}
其他的
{
console.log(“享受游戏”);
}
第一个条件检查后有一个分号。此外,您应该始终将条件分支的开始括号与括号放在同一行上
if (userAnswer === "yes");
删除分号。var-age;
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");
}
年龄=提示(“你多大了?”);
if(年龄<18岁)
{
警惕(“我们不采取任何行动或承担任何责任。风险自负!”);
}
否则,如果(年龄>18岁)
{
警惕(“享受游戏”);
}
为什么会这样?为什么我不能有一个?这像是结束语句吗?用代码>您正在告诉脚本停止呈现语句条件的其余部分。这是因为if()
,它必须具有从{}
开始的条件。如果If()
没有看到条件语句,它将停止渲染。如果使用JS,调试将更容易。好的答案应该包括对代码的解释,而不仅仅是代码。