From a5f0a36ba30cf4b57fcd47f4e2dca04e5aaa8320 Mon Sep 17 00:00:00 2001 From: Vaishnavi Date: Thu, 10 Feb 2022 16:06:19 +0530 Subject: [PATCH 1/2] problem resolve --- js/index.js | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/js/index.js b/js/index.js index f0898be..1e21646 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,41 @@ 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; + + 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 +80,4 @@ el.addEventListener("change", function(e) { el.addEventListener("input", function(e) { const userInput = e.target.value; drawGhostInput(userInput); -}); +}); \ No newline at end of file From 1e784f8507557ede883c39b75a4e6086c35adafa Mon Sep 17 00:00:00 2001 From: Vaishnavi Date: Thu, 10 Feb 2022 16:14:29 +0530 Subject: [PATCH 2/2] update file --- js/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/js/index.js b/js/index.js index 1e21646..8748472 100644 --- a/js/index.js +++ b/js/index.js @@ -30,6 +30,7 @@ function registerGuess(guess) { guess.split("").forEach(function(letter, index) { // TODO: handle additional letters when there are duplicates let letterStatus; + // this is for storing status const existsInWord = count[letter] > 0; const isInPlace = WORD_LETTERS[index] === letter;