-1

因此,我的程序将随机单词打印到控制台,以便我可以检查该单词是用于测试和实现的,因此当我测试代码并猜测正确的正确字母时,单击猜测按钮后单词占位符不会更新只是循环通过刽子手的阶段。这是我的程序代码;

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

  function newGame()
  {

    placeholder = "";
    goesLeft = 10;
    var word = wordsArray[Math.floor(Math.random() * wordsArray.length)];
    currentWord = 0;
    word = wordsArray[currentWord];
    wordLength = currentWord.length;
    wordSubstring = currentWord.substring;
    console.log(word);
    var myElement = document.getElementById("button").innerHTML = "Click to guess";
    var myPicElement = document.getElementById("hangimage").src = "http://fetlar.kingston.ac.uk/pp/hangman10.jpg";

    for (var count = 0; count < word.length; count++)
    {
      placeholder = placeholder + "-";
    }

    document.getElementById("placeholder").innerHTML = placeholder;
    document.getElementById("gamestatus").innerHTML = "Game running";
  }

  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();
    }
  }
newGame();
document.getElementById("button").onclick = guessLetter;

这是我的占位符代码,问题必须存在,程序也运行良好,我没有收到任何错误。

 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;
  }
}

这是链接到代码的 HTML;

<div style="text-align: center;">
   <h1>Hangman</h1>
   <img id="hangimage" src="http://fetlar.kingston.ac.uk/pp/hangman9.jpg" />
   <div id="placeholder" style="font-size: 1.5em; font-family: monotype; color: red;">-------</div>
   <div id="gamestatus">Game running</div>
   <input id="guessinput" type="text" size="1" />
   <button id="button">Click to guess</button>
   <br/>
   <div>Guessed Letters</div>
   <div id="guessedletters">(guessed letters will go here)</div>
</div>

如果有人能告诉我问题出在哪里,那就太好了,在此先感谢。

4

2 回答 2

0

这里有2个问题:

首先,你的 word 变量是全局的,所以不要在你的newGame()函数中重新定义它

var word = wordsArray[Math.floor(Math.random() * wordsArray.length)];

应该

word = wordsArray[Math.floor(Math.random() * wordsArray.length)];

第二个问题在这里:

wordLength = currentWord.length;

应该

wordLength = word.length;

这是一个编辑过的 jsFiddle :

http://jsfiddle.net/vj2rx/

于 2013-04-20T12:14:32.200 回答
0

给你,这是适合你的工作代码。

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

  function newGame(){
     placeholder = "";
     goesLeft = 10;
     word = wordsArray[getRandom(0,wordsArray.length)];
     wordLength = word.length;
     console.log(word);
     var myElement = document.getElementById("button").innerHTML = "Click to guess";
     var myPicElement = document.getElementById("hangimage").src = "http://fetlar.kingston.ac.uk/pp/hangman10.jpg";

     for (var count = 0; count < word.length; count++)
     {
          placeholder = placeholder + "-";
     }

     document.getElementById("placeholder").innerHTML = placeholder;
     document.getElementById("gamestatus").innerHTML = "Game running";
  }
  function getRandom(min,max){
    return Math.floor(Math.random() * (max - min) + min);
  }
  function guessLetter()
  {
     var correct = 0;

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


     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();
    }
  }
newGame();
document.getElementById("button").onclick = guessLetter;

我建议你应该为此开设一个课程,它会更好。

于 2013-04-20T12:31:39.347 回答