Skip to content
dreamcode
dreamcode
Projects
Text Stats
Guided projectJavaScript
Reward: +220 XP
PROJECT BRIEF

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 whitespace
  • sentences: the number of sentences. A sentence ends with one or more of ., ! or ?, and a final sentence with no ending mark still counts
  • longestWord: the longest word with punctuation stripped from its edges (the first one on a tie), or "" for empty text
Build it step by step
  1. Split on whitespace with text.trim().split(/\s+/) and drop empty strings.
  2. Split on /[.!?]+/ and count the pieces that still contain text after trimming.
  3. 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 sentences
    textStats("The sky is wide. Clouds drift by!")
    expected { words: 7, sentences: 2, longestWord: "Clouds" }
  • empty text
    textStats("")
    expected { words: 0, sentences: 0, longestWord: "" }
  • no ending mark
    textStats("just one thought")
    expected { words: 3, sentences: 1, longestWord: "thought" }
  • extra spaces and marks
    textStats(" Wait... what?! Amazing. ")
    expected { words: 3, sentences: 3, longestWord: "Amazing" }
On the line
+220 XP
Pass all 4 tests to claim it.