Practice

Problem 2 of 12

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 (a to z) and digits (0 to 9). 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

  1. Task 1, not checked yet. Make countWords pass the example tests.

  2. Task 2, not checked yet. Make countWords pass the hidden tests.

Task 1 of 2

Make countWords pass the example tests.

All tasks

main.js

Ctrl+Enter runs your code. Ctrl+Shift+Enter runs all the tests. Press Esc, then Tab, to leave the editor.

Output

Press Run to see your result

Your output shows here. Press Run to try your code.