Intermediate
Word Counter
countWords builds a dictionary from each word to how many times it appears. Strict mode reports counts[word] because {} has no keys.
Give counts a dictionary type so the code type-checks. The checks also make sure that looking up a word that isn't in the result has the type number | undefined, not plain number.
What Check tests
- Your code has no type errors
- The console prints
{ to: 2, be: 2, or: 1, not: 1 } - A result value can be read as a number-or-undefined
- A looked-up count is not assumed to exist
Stuck? One hint at a time
The solution unlocks after your first Check. Try first: that is where the learning happens.
Runs in your browser. Your code is saved here.
Press Run to see the page.
Console
Nothing printed yet.