Workshop › Wordle-style Word Guess › Step 14 of 26
Yellow means "in the word, somewhere else". The catch is repeated letters: each letter of the word can be used only once. So we count the word's letters not taken by a green, and hand out yellows from that count.
```js
// Mark each letter: green in the right place, yellow somewhere else in the word, gray not (or not that many times).
function score(guess, word) {
const marks = Array(5).fill('gray')
const left = {} // letters of the word not matched by a green
for (let i = 0; i < 5; i++) {
if (guess[i] === word[i]) marks[i] = 'green'
else left[word[i]] = (left[word[i]] || 0) + 1
}
for (let i = 0; i < 5; i++) {
if (marks[i] !== 'green' && left[guess[i]] > 0) {
marks[i] = 'yellow'
left[guess[i]] -= 1
}
}
return marks
}
```left is an object used as a counting table: left['r'] is how many rs of the word are still free.(left[x] || 0) + 1 adds one, starting from 0 the first time (when left[x] is undefined).Write the comment above score, add the left line and the else line in the first loop, and write the second loop
above return marks.
Skills: Arrays and objects, Loops
← Right letter, right place Enter sends the guess →