Workshop › Wordle-style Word Guess › Step 23 of 26
The on-screen keyboard can do what a real one cannot: colour each key with the best thing known about its letter, so you see at a glance which letters are left. Green beats yellow beats gray, so each mark gets a rank.
```js
// The best thing known about each letter so far: green beats yellow beats gray.
function letterColors() {
const rank = { gray: 1, yellow: 2, green: 3 }
const known = {}
for (const g of guesses) {
;[...g.word].forEach((letter, i) => {
const mark = g.marks[i]
if (!known[letter] || rank[mark] > rank[known[letter]]) known[letter] = mark
})
}
return known
}
const known = letterColors()
ctx.fillStyle = known[k.key] ? COLORS[known[k.key]] : '#71717a'
```rank orders the marks. known maps a letter to its best mark so far.OK and DEL never are).function draw() {, write the comment and letterColors, and leave an empty line.draw, above ctx.font = 'bold 14px sans-serif', write const known = letterColors().fillStyle line. Press Run and send a guess.Skills: Arrays and objects, Game states
← Enter and Delete keys Keep statistics →