数字猜谜游戏中的全局变量

时间:2018-09-29 16:41:35

标签: javascript global-variables local-variables

我正在尝试制作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>

1 个答案:

答案 0 :(得分:0)

在脚本中,您致电

parseInt(guessField.value) // effectively that is parseInt("") when it's empty

使用空字符串returns NaN调用parseInt();

在示例中使用MDN:

var userGuess = Number(guessField.value);

Number("")返回0值。

每次调用userGuess时,还需要更新checkGuess()的值。因此,您需要进行以下更改:

// ... 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中添加了一些注释,以获取更多解释。