Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

duplicate character problem resolve #11

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions js/index.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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;
}
Expand Down Expand Up @@ -59,4 +81,4 @@ el.addEventListener("change", function(e) {
el.addEventListener("input", function(e) {
const userInput = e.target.value;
drawGhostInput(userInput);
});
});