我做了一个随机数猜测游戏,你可以在其中确定最大值和最小值。我检查了我的整个代码两次,但似乎无法找到解决方案,为什么它不起作用。我将这个脚本链接到解释document.getElementById("randNum").innerHTML
部分的HTML文件。 我也想说我对编码很陌生,所以我可能忽略了一些简单的东西。无论如何,这是我的代码。这个随机数猜测游戏有什么问题?
var max = prompt("Max guessing number?");
if (max <= 2) {
document.getElementById("randNum").innerHTML = "Invalid entry";
} else {
var min = prompt("What's the smallest number you can guess?");
if (min >= (max - 1) || min <= 0) {
document.getElementById("randNum").innerHTML = "Invalid entry";
} else {
number = Math.floor(Math.random() * (max - min + 1) + min);
guess = prompt("What's your guess?");
if (guess > max || guess < min) {
document.getElementById("randNum").innerHTML = "Invalid entry";
} else if (guess == number) {
document.getElementById("randNum").innerHTML = "Good job! You guessed the number!";
} else {
document.getElementById("randNum").innerHTML = "Wrong! The answer was " + number + "!";
}
}
}
编辑:我改变了随机数生成器的工作方式,但我仍然收到错误。而添加更多细节,页面上显示无效项
您是否在浏览器控制台中看到错误? – Andy