0

在我的猜测字母函数中,我在单词变量的子字符串和长度方法中遇到错误,因为它说它是未定义的,但它在这里明确定义

    var wordsArray = ["monitor", "program", "application", "keyboard", "javascript", "gaming", "network"];
var word;
var goesLeft;
var placeholder;
var input;
var wordLength;
var wordSubstring;

为了解决这个问题,我尝试为 word.Substring 和 word.Length 创建变量并在函数中定义它们的值,但我仍然一无所获

这是猜测字母的功能

    function guessLetter()
{
  var correct = 0;

  var inputBox = document.getElementById("guessinput");
  input = inputBox.value;


  console.log(word);

  for (var count = 0; count < wordLength; count++)
  {
    if (input == word.substring(count, count + 1))
    {
      correct++;
      placeholder = placeholder.substring(0, count) + input + placeholder.substring(count + 1, placeholder.length + 1);
      document.getElementById("placeholder").innerHTML = placeholder;
    }
  }

  if (correct == 0)
  {
    goesLeft--;
  }
  var url = document.getElementById("hangimage").src = "http://fetlar.kingston.ac.uk/pp/hangman" + goesLeft + ".jpg";

  if (placeholder == word)
  {
    document.getElementById("hangimage").src = "http://fetlar.kingston.ac.uk/pp/hangman_win.jpg";
    alert("You guessed the word correctly.You win!");
  }

  if (goesLeft == 0)
  {
    alert("You lose");
    newGame();
  }
}

错误在这里出现

if (input == word.substring(count, count + 1))

以及定义 for 循环的地方

错误显示“事件期间检测到错误:无法调用未定义的方法‘子字符串’”

有任何想法吗?提前谢谢了

4

1 回答 1

0

从您发布的代码中,您从未将 word 设置为值。

var word; <-- that is undefined

我希望在您的代码中看到

currentWord = 0;
word = wordsArray[currentWord];
于 2013-04-19T14:02:21.527 回答