Skip to curriculum
Jump to the editor

2.10. Count words and find the most frequent character

Easy
Topics

This question is in

2. Strings and text
10–20 min

Editors show a word count, forms limit a bio to 100 words, and text tools show which letters appear most. Both questions sound simple and both have traps: splitting on spaces counts "--" as a word and " " as two empty ones, [a-z] knows only English letters, and when two characters appear equally often the answer should not depend on luck.

Write two functions in text-stats.js: countWords and mostFrequentChar.

Signature

JAVASCRIPT
function countWords(text)                                  // a number
function mostFrequentChar(text, { ignoreCase = false } = {}) // { char, count } or null

Requirements

  1. countWords counts the words in text. A word starts with a letter or a digit, in any language, and continues with letters, digits and combining marks (accents).
  2. Spaces, tabs and new lines never make extra words, so an empty or blank text has 0.
  3. Punctuation is not a word. Hyphens and slashes separate words: "well-known" is 2.
  4. An apostrophe (' or ’) with a letter or digit on both sides stays inside its word: "don't" is 1. An apostrophe on its own or at the edge of a word is punctuation.
  5. Numbers are words; emoji are not.
  6. countWords gives the same answer every time it is called, and handles 50,000 words quickly.
  7. mostFrequentChar returns { char, count } for the character (code point) that appears most often, not counting whitespace. An emoji counts as one character; punctuation and digits count.
  8. On a tie, the character that appears first in text wins.
  9. It returns null when text has no character other than whitespace.
  10. By default, upper and lower case are different characters. With ignoreCase: true, they are compared in lower case and char is the lower-case form.
  11. Each call starts from zero, and a text of 200,000 characters is handled quickly.

Examples

JAVASCRIPT
countWords("  Hello,   world! ");   // 2
countWords("don't stop");           // 2
countWords("well-known");           // 2
countWords("привет мир");           // 2
countWords(" -- ");                 // 0
JAVASCRIPT
mostFrequentChar("banana");                      // { char: "a", count: 3 }
mostFrequentChar("abba");                        // { char: "a", count: 2 }: a appears first
mostFrequentChar("AaB", { ignoreCase: true });   // { char: "a", count: 2 }
mostFrequentChar("   ");                         // null

Environment

  • Plain JavaScript in a browser page; the tests call the globals countWords and mostFrequentChar from text-stats.js.
  • text is always a string.
  • Any built-in may be used.

Hints

  1. Is it easier to describe the gaps between words, or the words themselves?
  2. Which regular-expression flag lets \p{…} match a letter in any script?
  3. In what order does a Map list its keys?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…