我正在学习JS的CodeAcademy课程,但是有一个问题,脚本是有效的,但是我
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.
发布于 2016-04-07 21:06:03
我猜它可能是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.'");
}发布于 2016-04-07 21:02:57
您的代码可以在浏览器中运行,我可以在浏览器控制台窗口中看到结果,所以我认为您应该将此报告为bug。
但是,您只需要检查小写的yes。Yes或YES将与if语句不匹配,并将运行else块中的代码。
您可以尝试:
if(userAnswer.toLowerCase() === "yes") {
// Your code here
} else {
// Your code here
}发布于 2016-04-07 21:14:49
有时,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.'"
);https://stackoverflow.com/questions/36476936
复制相似问题