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.
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).
Spaces, tabs and new lines never make extra words, so an empty or blank text has 0.
Punctuation is not a word. Hyphens and slashes separate words: "well-known" is 2.
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.
Numbers are words; emoji are not.
countWords gives the same answer every time it is called, and handles 50,000 words quickly.
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.
On a tie, the character that appears first in text wins.
It returns null when text has no character other than whitespace.
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.
Each call starts from zero, and a text of 200,000 characters is handled quickly.