我正在codecademy上学习javascript。下面的程序在我提交的时候卡住了。我对此很陌生,所以我找不到该错误。我下载了Aptana Studio,但我不知道如何调试:(。有什么方法可以跟踪代码吗?提前致谢。
var slaying = true;
var youHit = Math.random() > 0.5;
var damageThisRound = Math.floor(5 * Math.random());
var totalDamage = 0;
while (slaying) {
if (youHit) {
console.log("You hit the dragon.");
totalDamage += damageThisRound;
if (totalDamage >= 4) {
console.log("You've stew the dragon!");
slaying = false;
} else {
youHit = Math.random() > 0.5;
}
} else {
console.log("The dragon defeated you.")
}
}
最佳答案
据我了解。您需要在else部分设置slaying = false
,否则程序将陷入无限循环。
} else {
slaying = false; //Added here - Breaks the while() condition
console.log("The dragon defeated you.")
}
很简单,当龙打败了你。
杀戮就停止了。 (双关语)
要进行调试,请使用 Chrome 内置的开发人员工具
或 Firefox 中的Firebug
。两者都可以在您选择的浏览器中使用 F12
进行访问。
关于javascript:程序卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19209661/