语法错误:意外的令牌Else

时间:2014-02-01 16:18:19

标签: 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 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");
}

3 个答案:

答案 0 :(得分:1)

if (userAnswer === "yes");

删除分号。

答案 1 :(得分:0)

第一次条件检查后有一个分号。此外,您应始终将条件分支的左括号放在与括号

相同的行上

答案 2 :(得分:-1)

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");
}