我正在尝试制作MDN在JS教程中提供的标准猜号游戏。我尝试对其进行一些调整,为不同的场景制作不同的功能。
似乎全局变量var userGuess = parseInt(guessField.value)
不起作用,因为您之前的猜测总是以NaN
出现。
此外,当游戏重置时,showWin()
和showLoss()
功能起作用,但showError()
功能不起作用。
我对JS和编码非常陌生,所以很可能在某个地方有一个愚蠢的错误,如果有人可以帮助我解决这个问题,那将不胜感激!
var randNum = Math.floor(Math.random() * 100) + 1;
var guessField = document.querySelector('.guessField');
var guessSubmit = document.querySelector('.guessSubmit');
var guesses = document.querySelector('.guesses');
var lastResult = document.querySelector('.lastResult');
var lowOrHi = document.querySelector('.lowOrHi');
var guessCount = 1;
var resetButton;
var userGuess = parseInt(guessField.value);
function checkGuess() {
if(guessCount === 1) {
guesses.textContent = "Previous Guesses: ";
}
guesses.textContent += userGuess + ' ';
if(userGuess === randNum) {
showWin();
} else if(guessCount === 10) {
showLoss();
} else {
showError();
}
guessCount++;
guessField.value = '';
guessField.focus();
}
guessSubmit.addEventListener('click', checkGuess);
function showWin() {
lastResult.textContent = 'You won nice job schmuck';
lastResult.style.backgroundColor = 'green';
gameOver();
}
function showError() {
lastResult.textContent = 'Sorry, wrong guess';
if(userGuess > randNum) {
lowOrHi.textContent = 'Your guess was too high';
} else if(userGuess < randNum) {
lowOrHi.textContent = 'Your guess was too low';
}
}
function showLoss() {
lastResult.textContent = 'You lost, you schmuck';
lastResult.style.backgroundColor = 'red';
gameOver();
}
function gameOver() {
guessField.disabled = true;
guessSubmit.disabled = true;
resetButton = document.createElement('button');
resetButton.textContent = 'New Game';
document.body.appendChild(resetButton);
resetButton.addEventListener('click', resetGame);
}
function resetGame() {
guessCount = 1;
var resetParas = document.querySelectorAll('.resultParas');
for(i = 0; i < resetParas.length; i++) {
resetParas[i].textContent = '';
}
guessField.disabled = false;
guessSubmit.disabled = false;
resetButton.parentNode.removeChild(resetButton);
lastResult.style.backgroundColor = 'white';
randNum = Math.floor(Math.random() * 100) + 1;
}
<h1>Guessing Game</h1>
<p>Type in a number between 1 and 100 and I will tell you if it is too high or low.</p>
<form>
<label for="guessField">Enter a guess: </label>
<input type="text" id="guessField" class="guessField"/>
<input type="button" value="Submit Guess" class="guessSubmit"/>
</form>
<div class='resultParas'>
<p class="guesses"></p>
<p class="lastResult"></p>
<p class="lowOrHi"></p>
</div>
在你的脚本中,你调用
parseInt(guessField.value) // effectively that is parseInt("") when it's empty
使用空字符串调用parseInt()
返回NaN
;
MDN 在他们的示例中使用:
var userGuess = Number(guessField.value);
Number("")
返回一个 0 数字值。
您还需要在每次调用checkGuess()
时更新userGuess
的值。因此,您需要的更改是:
// ... code
var userGuess = Number(guessField.value);
// ... the rest of code
function checkGuess() {
userGuess = Number(guessField.value)
// ... rest of code
}
// rest of code
当然,您不必使用Number()
,您也可以进行其他一些条件检查,但Number()
是接受数字或空字符串的优雅方式。
在此处更新新的jsbin。
对于resetGame()
部分:您正在选择如下.resultParas
:
var resetParas = document.querySelectorAll('.resultParas');
然后,您遍历了该结果并替换了这些元素的.textContent
。但这些不是简单的文本节点,它们是包含文本节点的释义节点。我把它改成:
var resetParas = document.querySelector('.resultParas').children;
它应该可以工作!我在jsfiddle中放了一些评论以获得更多解释。