wordle_clone
wordle_clone copied to clipboard
Double letter guess
In a guess that contains two of the same letter where one letter exists in the finished word, the code labels the first instance of the letter, and therefore, even if the second is in the correct place, it is marked incorrect.
Ex: Guess: husks h(green), u(green), s(yellow), k(green), s(black) Answer: hunks
I would expect the first 's' to be black and the second to be green.
i know this is really late, but I made a Wordle clone too and it doesn't have this problem. Probably not the most efficient/cleanest code, but it does the job https://github.com/Trollermaner/Wordle
The problem is in the checkGuess function. With each pass through the for loop, the guessString is compared letter by letter to the rightGuess array. If there is a match, the rightGuess array character is changed to a '#'. With a guess containing 2 of the same letter, by the time the second letter is tested, the true letter in the rightGuess array is no longer present and results in a no match and is color changed to grey. Since the rightGuess array doesn't have to save old matches, the fix is to just add a line at the top of the 'for' loop to recreate the rightGuess array. This is shown as follows:
function checkGuess () { let row = document.getElementsByClassName("letter-row")[6 - guessesRemaining] let guessString = '' let rightGuess = Array.from(rightGuessString)
for (const val of currentGuess) {
guessString += val
}
if (guessString.length != 5) {
toastr.error("Not enough letters!")
return
}
if (!WORDS.includes(guessString)) {
toastr.error("Word not in list!")
return
}
for (let i = 0; i < 5; i++) {
rightGuess = Array.from(rightGuessString) //ADD THIS LINE
let letterColor = ''
let box = row.children[i]
#2
I have solved this very efficiently in my pull request. Please checkout and merge it.