1 回答
TA贡献1877条经验 获得超6个赞
您需要摆脱 while 循环。您只是循环而不允许用户更新输入文本框。您应该检查每次点击的次数。
var guessbutton = document.getElementById("guessbutton");
var count = 0;
var maxTries = 5;
var secret = 10;
function abc() {
var guess = Number(document.getElementById("guess").value);
count++;
console.log(count);
if (guess == secret) {
document.getElementById("feedback").innerHTML = "correct";
guessbutton.disabled = true;
} else if (guess > secret) {
document.getElementById("feedback").innerHTML = "too high";
} else if (guess < secret) {
document.getElementById("feedback").innerHTML = "too low";
}
if (count >= maxTries) {
document.getElementById("feedback").innerHTML = "max try";
guessbutton.disabled = true;
}
}
<input id="guess" type="text" name="guess">
<button id="guessbutton" type="button" onclick="return abc()">Guess</button>
<p id="feedback"></p>
添加回答
举报