codeacademy if / else javascript

时间:2016-04-07 12:58:22

标签: javascript if-statement

我正在遵循CodeAcademy for JS的课程,但是有一个问题,脚本工作但我



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




给我错误:

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

此处页面:https://www.codecademy.com/courses/javascript-beginner-en-x9DnD/0/5?curriculum_id=506324b3a7dffd00020bf661,代码自己冒险! 5。

5 个答案:

答案 0 :(得分:2)

我猜它可能是Codeacadamey中的bug。奇怪的是,下面的代码可以运行并给我一个绿色图标。

编写if,else if和else语句可以解决这些问题。可能是因为说明不是很清楚。

// Check if the user is ready to play!
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 if (userAnswer === 'no') {
    console.log("Oh no! Bieber shakes his head and sings 'I set a pace, so I can race without pacing.'");
} else {
    console.log("Oh no! Bieber shakes his head and sings 'I set a pace, so I can race without pacing.'");
}

答案 1 :(得分:0)

您的代码在浏览器中适用于我,我可以在浏览器控制台窗口中看到结果,因此我认为您应该将此报告为错误。

但是,您只检查小写yesYesYESif语句不匹配,并会在else块中运行代码。

你可以尝试:

if(userAnswer.toLowerCase() === "yes") {    
    // Your code here
} else {
    // Your code here
}

答案 2 :(得分:0)

有时,Codeacademy会根据琐碎的空白差异错误地报告代码中的错误。根据脚本的输出,您已正确编写代码。

有趣的是,使用ternary operator代替if语句可以正常工作:

// Check if the user is ready to play!

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

console.log( userAnswer === "yes" ?
    "You and Bieber start racing. It's neck and neck! You win by a shoelace!" :
    "Oh no! Bieber shakes his head and sings 'I set a pace, so I can race without pacing.'"
);

答案 3 :(得分:0)



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




答案 4 :(得分:0)

内容应如下所示,您可能已删除了某些内容。

// Check if the user is ready to play!
confirm('I am ready to play');
var age = prompt("What's your age");

if(age < 13){
    console.log("They're allowed to play but you take no responsibility");
} else {
    console.log("Play On");
}
// Check if the user is ready to play!
var introduction  = "You are at a Justin Bieber concert, and you hear this lyric 'Lace my shoes off, start racing.'";

console.log(introduction);
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.'");
}