找到正确答案后,我似乎无法使此代码提醒用户,也无法使弹出窗口继续循环播放。我将此编码为学习练习。在JS中,我非常菜鸟。
有人可以审查我的代码并提出建设性的批评吗?
// noprotect
var targetNumber = Math.floor(Math.random() * 100 + 1);
var userGuess = prompt("Pick a number from 1 to 100 to guess which one I'm thinking about!" + " (6 guesses left.)");
for (var i=5; i>0; i--) {
if (i === 0) {
prompt("Out of guesses!" + " (" + i + " guesses left.)" + " My number was: " + targetNumber);
}
if (isNaN(userGuess) === true) {
userGuess = prompt("That value was not a number! Please pick a number from 1 to 100 to guess which one I'm thinking about!" + " (" + i + " guesses left.)");
}
if (userGuess < 1 || userGuess > 100) {
userGuess = prompt("That number was not between 1 and 100 inclusive! Please pick a number from 1 to 100 to guess which one I'm thinking about!" + " (" + i + " guesses left.)");
}
if (userGuess === targetNumber) {
userGuess = alert("You're correct! My number was: " + targetNumber);
}
if (userGuess < targetNumber) {
userGuess = prompt("You're too low, guess again" + " (" + i + " guesses left.)");
}
if (userGuess > targetNumber) {
userGuess = prompt("You're too high, guess again!" + " (" + i + " guesses left.)");
}
}
最佳答案
在我从办公室回家之前,只想简单说明一下。您的for
循环将永远不允许i === 0
条件通过,因为该循环仅在i > 0
时继续。您可能希望将其更改为i >= 0
或在循环中调整条件。
关于javascript - Javascript数字猜谜游戏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31573976/