Problem · Easy · 15 min
Count the words
- Easy
- Hash maps
- Strings
Write countWords(text). It returns an object. Each key is a word from the text. Each value is the number of times that word appears.
- A word is a run of letters (
atoz) and digits (0to9). Any other character ends a word. Spaces, commas and dots end a word, and so do'and-. - The case of a letter does not matter. Write each key in lower case.
- If the text has no words, return an empty object.
Examples
| Call | Result |
|---|---|
countWords('the cat and the hat') |
{ the: 2, cat: 1, and: 1, hat: 1 } |
countWords('Go, go, GO!') |
{ go: 3 } |
countWords('') |
{} |
Tasks
2 to do
Task 1, not checked yet. Make
countWordspass the example tests.Task 2, not checked yet. Make
countWordspass the hidden tests.
Task 1 of 2
Make countWords pass the example tests.