JavaScript的!创建猜猜游戏这个词

时间:2017-12-13 07:04:47

标签: javascript html loops

此处的代码:

  let ccdisplay = document.querySelector('.crrDisplay');
  let incdisplay = document.querySelector('.incDisplay');
  let guess = document.querySelector('#character');
  let textForm = document.querySelector('.textForm');

           var commonWords = [
        "the", "of", "and", "a", "to", "in", "is", "you", "that", "it", 
        "he", "was", "for", "on", "are", "as", "with", "his", "they","I", "at", 
        "be","this", "have", "from", "or", "one", "had", "by", "word", "but","not",
        "what", "all", "were", "we", "when", "your", "can", "said", "there",
        "use", "an", "each", "which", "she", "do", "how", "their", "if", 
        "will","up", "other", "about", "out", "many", "then", "them",
        "these", "so","some", "her", "would", "make", "like", "him", "into", "time", "has",
         "look", "two", "more", "write", "go", "see", "number", "no", "way",
        "could", "people", "my", "than", "first", "water", "been", "call",
        "who", "oil", "its", "now", "find", "long", "down", "day", "did", 
        "get", "come", "made", "may", "part"];


  // Grabbing Random Word
  var chooseRandomWord = function(array) {
      return array[Math.floor(Math.random() * array.length)];
  }

  var chosenWord = chooseRandomWord(commonWords);
  console.log(chosenWord)

  // Function that submits the values
  textForm.addEventListener('submit', function(event) {

  var counter = 10;
  var triedCharacters = [];
  var correctCharacters = [];

  event.preventDefault();
  guess = character.value    

  for (i = 0; i < chosenWord.length; i++) {
      chosenWord[i]
      for (z = 0; z < guess.length; z++) {
          if (guess[z] === chosenWord[i]) {
              correctCharacters.push(guess[z])
              console.log("correct " + correctCharacters)
          } 
          else {
              triedCharacters.push(guess[z])
              console.log("incorrect " + triedCharacters)
          }
      };
  }
  })

嘿,我正在尝试创建一个猜测随机单词的游戏并将正确的单词放在一个数组中,而另一个数组中的错误字符正确的数组正常工作但不正确的其他单词不起作用推动每一个角色。

1 个答案:

答案 0 :(得分:1)

应该只有一个循环。你也应该通过较短的单词循环,但是一个单词长度的提示会很好......

let display = document.querySelector('.display');
let guessQuerySelector = document.querySelector('#character');
let textForm = document.querySelector('.textForm');

var commonWords = [
  "the", "of", "and", "a", "to", "in", "is", "you", "that", "it", "he",
  "was", "for", "on", "are", "as", "with", "his", "they", "I", "at", "be",
  "this", "have", "from", "or", "one", "had", "by", "word", "but", "not",
  "what", "all", "were", "we", "when", "your", "can", "said", "there",
  "use", "an", "each", "which", "she", "do", "how", "their", "if", "will",
  "up", "other", "about", "out", "many", "then", "them", "these", "so",
  "some", "her", "would", "make", "like", "him", "into", "time", "has",
  "look", "two", "more", "write", "go", "see", "number", "no", "way",
  "could", "people", "my", "than", "first", "water", "been", "call",
  "who", "oil", "its", "now", "find", "long", "down", "day", "did", "get",
  "come", "made", "may", "part"
];

// Grabbing Random Word
var getRandomWord = function(array) {
  return array[Math.floor(Math.random() * array.length)];
}

var randomWord = getRandomWord(commonWords);
console.log('randomWord', randomWord);

// Function that submits the values
textForm.addEventListener('submit', function(event) {
  event.preventDefault();

  var counter = 10;
  var triedCharacters = [];
  var correctCharacters = [];

  var guessWord = guessQuerySelector.value;
  var shorterWordlength = randomWord.length > guessWord.length ? guessWord.length : randomWord.length;

  console.log('guessWord', guessWord);

  for (i = 0; i < shorterWordlength; i++) {
      if (guessWord[i] === randomWord[i]) {
        correctCharacters.push(guessWord[i])
        console.log("correct " + correctCharacters)
      } else {
        triedCharacters.push(guessWord[i])
        console.log("incorrect " + triedCharacters)
      }
  }
  randomWord = getRandomWord(commonWords);
  console.log('randomWord', randomWord);
})