javascript noob level语法错误

时间:2014-02-20 12:52:46

标签: javascript

var age = Number(prompt("How old are you?"));

if(age >= 13) {
    console.log("You are old enough!");
    alert("You Are old enough!");
    console.log("You are at a Justin Bieber concert, and you hear this lyric 'Lace my shoes off, start racing.'");
    console.log("Suddenly, Bieber stops and says, 'Who wants to race me?'");

    var userAnswer = prompt("Do you want to race Bieber on stage?");
    if(userAnswer) "yes";
    {
        console.log("You and Bieber start racing. It's neck and neck! You win by a shoelace!");
    } else {
        console.log("Oh no! Bieber shakes his head and sings 'I set a pace, so I can race without pacing.'");
    }
} else {
    console.log("Sorry, You Are Not old Enough :( ");
    alert("Sorry, You Are Not old Enough :( ");
}

我得到了:

  

SyntaxError:意外的标记'else'

如果有人能帮助我,那就太棒了:)

6 个答案:

答案 0 :(得分:3)

您需要更改

if (userAnswer) "yes";
{
}

if (userAnswer == "yes")
{
}

为什么?

声明if (userAnswer) "yes";表示检查userAnswer是否有值(if(userAnswer)),然后是"yes";的意外标记。

正确的答案如何运作?

if (userAnswer == "yes")表示使用==运算符检查userAnswer是否等于“是”,这是许多编程语言中常见的比较运算符。

提示:您可能希望使用if (userAnswer.toUpperCase() == "YES"),因为这意味着可以在任何情况下输入yes,例如yEsYEs ...

答案 1 :(得分:2)

这是针对提示,您需要将userAnswer与您的值进行比较:

if (userAnswer) "yes"; 

无效。改为

if (userAnswer == "yes")

但是,这只有在用户输入“是”时才有效,而不是“确定”,“是的!”或类似的。

为避免这种情况,您可以使用确认对话框,它将提供两个选项“确定”或“取消”。

if(confirm("Do you want to race Bieber on stage?")) {

答案 2 :(得分:2)

你的第二个if语句中有错误。

if (userAnswer == "yes");

答案 3 :(得分:1)

删除if循环中的;

age = prompt(" How old Are You ? ");
if( age >= 13) {
    console.log("You Are old Enough ! ");
    alert("You Are old Enough ! ");
    console.log("You are at a Justin Bieber concert, and you hear this lyric 'Lace my shoes off, start racing.'");
    console.log("Suddenly, Bieber stops and says, 'Who wants to race me?'");
    var userAnswer = prompt("Do you want to race Bieber on stage? ");
    if (userAnswer=="yes"){
        console.log("You and Bieber start racing. It's neck and neck! You win by a shoelace!");
   } else {
        console.log("Oh no! Bieber shakes his head and sings 'I set a pace, so I can race without pacing.'");
    }
} else {
    console.log("Sorry, You Are Not old Enough :( ");
    alert("Sorry, You Are Not old Enough :( ");
}

答案 4 :(得分:0)

prompt更改为confirm并检查随后返回的布尔值会更加健壮。

使用prompt将返回一个字符串,您明确必须检查某个值(例如,通过正则表达式或一组可能的有效答案)。如果用户输入For sureHell, yeah!或甚至简单YES,则您的支票userAnswer == "yes"将失败。

这样可以更好地使用它:

var userAnswer = confirm("Do you want to race Bieber on stage? ");

if (userAnswer){
   /*...*/

答案 5 :(得分:-1)

由于显然没有人知道提示返回字符串,所以我们转到:

age = prompt(" How old Are You ? ");
if( age >= 13) {
    console.log("You Are old Enough ! ");
    alert("You Are old Enough ! ");
    console.log("You are at a Justin Bieber concert, and you hear this lyric 'Lace my shoes off, start racing.'");
    console.log("Suddenly, Bieber stops and says, 'Who wants to race me?'");
    var userAnswer = prompt("Do you want to race Bieber on stage? ");
    if (userAnswer == "yes") {
        console.log("You and Bieber start racing. It's neck and neck! You win by a shoelace!");
   } else {
        console.log("Oh no! Bieber shakes his head and sings 'I set a pace, so I can race without pacing.'");
    }
} else {
    console.log("Sorry, You Are Not old Enough :( ");
    alert("Sorry, You Are Not old Enough :( ");
}