diff --git a/js/index.js b/js/index.js index f0898be..8748472 100644 --- a/js/index.js +++ b/js/index.js @@ -1,6 +1,6 @@ let attempts = 6; -const WORD = WORDS[Math.floor(Math.random() * WORDS.length)]; +const WORD = WORDS[Math.floor(Math.random() * WORDS.length)]; const el = document.querySelector("#guess"); console.log("Target:", WORD); @@ -9,20 +9,42 @@ function registerGuess(guess) { guess = guess.toUpperCase(); const status = []; const WORD_LETTERS = WORD.split(""); + var count = []; + window.count = count; + + WORD.split("").forEach(function(letter, index) { + Boolean(count[letter]) ? count[letter]++ : (count[letter] = 1); + //count[letter]++; + //console.log(count[letter] + " " + letter); + }); + guess.split("").forEach(function(letter, index) { + const isInPlace = WORD_LETTERS[index] === letter; + + if (isInPlace) { + count[letter]--; + } + + }) + //PEACE + //EPACE guess.split("").forEach(function(letter, index) { // TODO: handle additional letters when there are duplicates let letterStatus; - const existsInWord = WORD_LETTERS.indexOf(letter) > -1; + // this is for storing status + + const existsInWord = count[letter] > 0; const isInPlace = WORD_LETTERS[index] === letter; if (isInPlace) { letterStatus = 2; } else if (existsInWord) { letterStatus = 1; + count[letter]--; } else { letterStatus = 0; } status.push(letterStatus); }) + printGuess(guess, status); return status; } @@ -59,4 +81,4 @@ el.addEventListener("change", function(e) { el.addEventListener("input", function(e) { const userInput = e.target.value; drawGhostInput(userInput); -}); +}); \ No newline at end of file