javascript if / else not working

时间:2015-01-08 12:49:44

标签: javascript

我正在javascript codecadamy的第5课中运行代码。它与提示和if / else有关,所以我是java开发人员,看起来我的代码都是正确的,但它给出了错误

Oops, try again. Did you add an if statement to your code? 

教程指导如下:

  

现在你必须创建不同的场景。好的我们知道如何使用if / else语句来做到这一点。

     

如果userAnswer是"是",请打印出来:"你和比伯开始比赛。它的颈部和颈部!你用鞋带赢了!"

     

否则,打印出来:"哦不!比伯摇了摇头,唱着“我设定了节奏,所以我可以在没有节奏的情况下比赛。'"

     

请记住:=用于分配,===用于检查事情是否相等!

我的代码是

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

4 个答案:

答案 0 :(得分:0)

使用toLowerCase()或toUpperCase()方法:

if (userAnswer.toLowerCase()=='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.'");
}

答案 1 :(得分:0)

我已经尝试过一些东西,这个教程肯定是错误的(因为代码完全不正常),其他人也在Q& A部分抱怨,没有为此发布的解决方案。它在一年前工作,但它的文字略有不同。

因此,暂时,您应该跳过本教程的这一部分,并将其报告为无效。

答案 2 :(得分:0)

here is the answer which is working 100% i got it  [https://www.codecademy.com/forum_questions/5303b63e52f8632b29004fe5][1]

  // Check if the user is ready to play!
   confirm("I am ready");
age = prompt("What is your age?");
if (age < 13)
{
console.log("You are too young to play, but I take no responsibility");
} 
else 
{
console.log("Play on!"); }
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.'"); }

答案 3 :(得分:-1)

终于找到了答案:

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