

Text Stats
Write textStats(text) that returns { words, sentences, longestWord } for a piece of writing:
words: the number of words, where words are separated by any amount of whitespacesentences: the number of sentences. A sentence ends with one or more of.,!or?, and a final sentence with no ending mark still countslongestWord: the longest word with punctuation stripped from its edges (the first one on a tie), or""for empty text
Build it step by step
- Split on whitespace with
text.trim().split(/\s+/)and drop empty strings. - Split on
/[.!?]+/and count the pieces that still contain text after trimming. - Strip punctuation from each word with
replace(/^[^\w']+|[^\w']+$/g, "")and keep the longest.
Examples
textStats("The sky is wide. Clouds drift by!")
→ { words: 7, sentences: 2, longestWord: "Clouds" }
textStats("")
→ { words: 0, sentences: 0, longestWord: "" }
project.js
JAVASCRIPT
Saved as you type
Tests
0 of 4 passing- •two sentencestextStats("The sky is wide. Clouds drift by!")expected { words: 7, sentences: 2, longestWord: "Clouds" }
- •empty texttextStats("")expected { words: 0, sentences: 0, longestWord: "" }
- •no ending marktextStats("just one thought")expected { words: 3, sentences: 1, longestWord: "thought" }
- •extra spaces and markstextStats(" Wait... what?! Amazing. ")expected { words: 3, sentences: 3, longestWord: "Amazing" }
On the line
+220 XP
Pass all 4 tests to claim it.
