我在Codecademy上学习javascript。然后我突然遇到了错误"哎呀,再试一次。确保在控制台上打印消息!"
var slaying = true
var youHit = Math.floor(Math.random()* 2)
var damageThisRound = Math.floor(Math.random()*5 + 1)
var totalDamage = 0
var dragonSlaying = function() {
while(slaying){
if(youHit){
console.log("You hit that bastard");
totalDamage += damageThisRound;
if(totalDamage >= 4){
console.log("The dragon has been slain");
slaying = false;
} else {
youHit = Math.floor(Math.random() * 2);
}
} else {
console.log("You have been defeated; you missed the slimy thing! Maybe next time.");
slaying = false;
}
}
slaying = false;
}
答案 0 :(得分:2)
所以我发现了:
以下是更正后的代码:
var slaying = true;
var youHit = Math.floor(Math.random()* 2);
var damageThisRound = Math.floor(Math.random()*5 + 1);
var totalDamage = 0;
while(slaying){
if(youHit){
console.log("You hit that bastard");
totalDamage += damageThisRound;
if(totalDamage >= 4){
console.log("The dragon has been slain");
slaying = false;
} else {
youHit = Math.floor(Math.random() * 2);
}
} else {
console.log("You have been defeated; you missed the slimy thing! Maybe next time.");
slaying = false;
}
}
祝其他课程好运!
答案 1 :(得分:0)
你必须意识到Codecademy希望你以特定的方式做事,尽管有时候方向可能很模糊。
工作代码,有时甚至不会因此而进入下一课。确保完全遵循他们的指示,并确保注意最后一步,有时包括调用该功能。
在代码末尾输入ps分号,将其视为结束语句或命令。如果你不以分号结束,代码只会运行到下一行,然后它就不再起作用了。