Workshop › Typing Rain › Step 26 of 33
Every key that did not help (a letter no word starts with, or the wrong next letter) is a mistake. Accuracy is the share of keys that were right.
```js
let letters // correct letters typed
let mistakes
letters = 0
mistakes = 0
if (options.length === 0) {
mistakes += 1
return
}
if (target.text[typed] !== key) {
mistakes += 1
return
}
typed += 1
letters += 1
const accuracy = () => (letters + mistakes === 0 ? 100 : Math.round((100 * letters) / (letters + mistakes)))
```returns become blocks that count a mistake first.letters.accuracy is 100 * letters / (letters + mistakes), rounded. Before any key it would divide by zero (0 / 0 is
NaN in JavaScript), so it returns 100 then.let best write let letters and let mistakes; at the end of reset, set both to 0.type, turn the two one-line returns into blocks that add a mistake, and add letters += 1 under typed += 1.keydown listener write accuracy, with an empty line after it.Skills: Game states, Functions
← Best score Words per minute →