I'm making a Wordle type game (you guess a word, if your guess has the right letter in the right spot the guessed letter should go green, if it is the right letter but the wrong spot it shoulod go yellow). I can't quite figure out the logic for colouring the squares when there are multiple letters in the guess.
Right now I have something like:
For X = 1 to 2
If GuessLetter(X) = WordLetter(X) then set GuessLetter(X) to green
Else if GuessLetter(X) is in WholeWord then set GuessLetter(X) to yellow.
Loop
However in the case of the word being AS and the guess being AA, this logic will set the first A green (right letter, right spot, then the second letter yellow (right letter, wrong spot). The correct result should be Green, no colour as there is only one A in the word.
What is the most efficient logic to stop a duplicate guess letter being coloured incorrectly yellow?
I am thinking something like count unique letters in the word and the guess, and skip letters in the guess that exceed the count of that letter in the word. But this does not feel very elegant. Is there a better way?
I'm doing this in javascript but interested in the general logic most of all.