猜数游戏中的全局变量

Global variable in number guessing game

我正在尝试制作 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() returns NaN

MDN 在他们的示例中使用:

var userGuess = Number(guessField.value);

Number("") returns 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() 是接受数字或空字符串的优雅方式。

更新 New jsbin here.

对于 resetGame() 部分:您选择的 .resultParas 如:

var resetParas = document.querySelectorAll('.resultParas');

然后您迭代了结果并替换了那些元素的 .textContent。但那些不是简单的文本节点,它们是段落节点,里面有文本节点。我将其更改为:

var resetParas = document.querySelector('.resultParas').children;

应该有用!我在 jsfiddle 中添加了一些评论以获得更多解释。