2017-12-13 1 views
0

만들기 여기에 코드입니다 :자바 스크립트! 추측에게 단어 게임

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

이봐, 난 임의의 단어를 추측 게임을 만들고 다른 배열 올바른에게 하나 개의 배열에 올바른 사람과 잘못된 문자를 넣어하려고 해요 배열이 작동하지만 잘못된 다른 문자가 작동하지 않고 모든 문자를 밀어 넣습니다.

+0

예상되는 출력은 얼마나됩니까? – klugjo

+0

전체 코드를 공유 하시겠습니까? – zabusa

+0

수도 main.js 60 올바르지 않은 main.js 60 잘못된 A, main.js는 Y : 60 잘못된 A, Y, Y main.js : 60 잘못된 A, Y (57)은 정정 main.js , y, a main.js : 57 correct a, y –

답변

0

루프가 하나만 있어야합니다. 단어 길이가 길어지면 짧은 단어를 반복해야하지만 단어 길이가 길면 좋습니다 ...

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); 
}) 
+0

예! 내가 어디로 잘못 갔는지 확인하고, 나는 정면을 고쳐 줄 것이다. 그래서 당신의 모든 도움을위한 힌트를 주어야한다! –