Skip to content
Exercise · Objects · lesson 11 of 13

Exercise: word count

Write a function function wordCount(text) that returns an object: its keys are the words of the string text in lower case, and its values are how many times each word occurs.

  • wordCount("the cat and the hat") → { the: 2, cat: 1, and: 1, hat: 1 }
  • wordCount("Go go GO") → { go: 3 }
  • wordCount("") → {}

Constraints: words consist of Latin letters in any case and are separated by one space; a word may be any combination of letters, constructor included; there are no spaces at the start or the end of the string; for the empty string the result is the empty object {}; the order of keys in the result does not matter.

Do not add extra imports unless the task says so explicitly.

word-count.js — javascript
javascript · 4 lines
Ctrl+Enter run · Esc leave editor

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way