Syntax Error: Unexpected token Else

笑着哭i 提交于 2019-12-24 00:39:32

问题


I am learning Javascript via Codecademy and no have been stumped on this little piece here.

I have supposed to write an if else statement.

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

回答1:


if (userAnswer === "yes");

Remove the semicolon.




回答2:


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




回答3:


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


来源:https://stackoverflow.com/questions/21500439/syntax-error-unexpected-token-else

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!