这是我网站的应用程序,我不太了解它的问题。我有同事在看它,我们无法弄清楚错误在哪里。
这应该是猜数字的游戏,其中会生成一个随机数,而用户应该猜数字;根据您与答案的接近程度,颜色会发生变化。但是,当单击该按钮时,它将弹出一个文件未找到错误。
const int = document.getElementById("Guess");
const LowOrHigh = document.querySelector("LowOrHigh");
const TimesGuessed = document.querySelector("TimesGuessed");
const WinOrLose = document.querySelector("WinOrLose");
let numberOfGuesses = 0;
const Color = document.querySelector("#Guess");
const RandomNumber = Math.random() * (100 - 1) + 1;
const win = "false";
const THEGODDAMBUTTON = document.getElementById("TreasureSubmitButton").addEventListener("click", Hey);
function Hey(int) {
let Guess = parseInt(int.valueOf);
if (RandomNumber === Guess) {
Color.style.backgroundColor = "#007F00";
Color.style.color = "#ffffff";
TimesGuessed.innerHTML('You have guessed ${numberOfGuesses} times');
win = "correct";
} else if (Guess > RandomNumber - 10 && Guess < RandomNumber || Guess < RandomNumber + 10 && Guess > RandomNumber) {
Color.style.backgroundColor = "#990000";
Color.style.color = "#ffffff";
numberOfGuesses++;
if (Guess > RandomNumber) {
LowOrHigh.innerHTML("Too high!");
} else if (Guess < RandomNumber) {
LowOrHigh.innerHTML("Too small!");
}
TimesGuessed.innerHTML('You have guessed ${numberOfGuesses} times');
WinOrLose.innerHTML("Good guess, try again!");
} else if (Guess > RandomNumber - 30 && Guess < RandomNumber || Guess < RandomNumber + 30 && Guess > RandomNumber) {
Color.style.backgroundColor = "#000099";
Color.style.color = "#ffffff";
numberOfGuesses++;
if (Guess > RandomNumber) {
LowOrHigh.innerHTML("Too high!");
} else if (Guess < RandomNumber) {
LowOrHigh.innerHTML("Too small!");
}
TimesGuessed.innerHTML('You have guessed ${numberOfGuesses} times');
WinOrLose.innerHTML("Good guess, try again!");
} else {
Color.style.backgroundColor = "#ffffff";
Color.style.color = "#000000";
numberOfGuesses++;
if (Guess > RandomNumber) {
LowOrHigh.innerHTML("Too high!");
} else if (Guess < RandomNumber) {
LowOrHigh.innerHTML("Too small!");
}
TimesGuessed.innerHTML('You have guessed ${numberOfGuesses} times');
WinOrLose.innerHTML("Good guess, try again!");
}
}
console.log(RandomNumber);
<div id="TreasureHunt">
<form action=submit method="post">
<fieldset id="guessFieldset">
<legend><strong>Please enter a number between 1 and 100</strong></legend>
<p>
<label for="Number:">Number:</label>
<input type="number" min="1" max="100" id="Guess" name="Guess">
</p>
<button type="submit" id="TreasureSubmitButton">Guess!</button>
<button type="submit" id="TreasureRestartButton">New game</button>
</fieldset>
<fieldset id="hintFieldset">
<legend><strong>Hint</strong></legend>
<p id="LowOrHigh"></p>
<p id="TimesGuessed"></p>
<p id="WinOrLose"></p>
</fieldset>
</form>
<p>In this game, your job is to guess the a number between 1 and 100! During the game, in the hint area, there will be displayed a color, if you are too high or too low and how many guesses you've had. Once you get the right number the hint area will shine
in green and you can restart the game by clickin new game. Try to guess wi the least guesses possible!</p>
</div>
答案 0 :(得分:0)
这里发生的事情。首先,未找到文件正在发生,因为它正在寻找名为“ submit”的文件,因为您具有:
<form action=submit method="post">
。您不需要此属性,也不需要method="post"
,因为您不会在任何地方发送表单数据。
第二件事是您没有将Hey()函数传递给int。由于您不使用邮政信息,因此表单数据不会随处可见。当您调用Hey()时,您需要传递document.getElementById('Guess').value
,因为您可以在其中传递它,或者执行以下操作:
// no args
function Hey() {
let Guess = document.getElementById('Guess').value;
希望有帮助!