Skip to curriculum
Jump to the editor

2.4. Truncate to a number of characters or words

Easy
Topics

This question is in

2. Strings and text
10–20 min

Cards, notifications, search results and table cells all show text in a fixed space: a title cut to 40 characters, a preview cut to 20 words, each ending with "…". The details decide whether it looks right: the ellipsis has to fit inside the limit, an emoji must not be cut in half into a broken symbol, and "Hello …" with a space before the ellipsis looks like a mistake.

Write two functions in truncate.js: truncateChars and truncateWords.

Signature

JAVASCRIPT
function truncateChars(text, maxLength, suffix = "…") // a string
function truncateWords(text, maxWords, suffix = "…")  // a string

Characters are counted as code points: "😀" is one character, though its length is 2. A word is a run of characters that are not whitespace.

Requirements

  1. truncateChars returns text unchanged when it has at most maxLength characters.
  2. Otherwise it keeps the first maxLength minus the suffix's length characters, removes any whitespace at the end of that part, and adds suffix. The result, suffix included, is never longer than maxLength.
  3. truncateChars never cuts an emoji in half, and counts an emoji as one character when deciding whether the text fits.
  4. A custom suffix ("...", " [+]", or "") is counted by its own length. maxLength may equal the suffix's length; then the result is just the suffix.
  5. truncateChars throws a RangeError when maxLength is not an integer, or is shorter than the suffix.
  6. truncateWords returns text unchanged when it has at most maxWords words.
  7. Otherwise it keeps the text up to the end of word number maxWords, with its original spacing and punctuation, and adds suffix. With maxWords 0, a text that has words gives just the suffix.
  8. truncateWords throws a RangeError when maxWords is not an integer of 0 or more, and handles a text of 50,000 words quickly.

Examples

JAVASCRIPT
truncateChars("Frontend design", 9);          // "Frontend…"
truncateChars("Hello world", 7);              // "Hello…": no space before the ellipsis
truncateChars("abcdefghij", 6, "...");        // "abc..."
truncateChars("I 😀😀😀 it", 4);              // "I 😀…"
truncateChars("Hello", 5);                    // "Hello"
JAVASCRIPT
truncateWords("The quick brown fox jumps", 3); // "The quick brown…"
truncateWords("Hello, world. How are you?", 2); // "Hello, world.…"
truncateWords("one two", 5);                    // "one two"
truncateWords("a b c", -1);                     // throws RangeError

Environment

  • Plain JavaScript in a browser page; the tests call the globals truncateChars and truncateWords from truncate.js.
  • text and suffix are always strings.
  • Characters made of several code points (a skin-tone emoji, a flag) are not part of the tests.
  • Any built-in may be used.

Hints

  1. What does "😀😀".slice(0, 3) return, and how can you slice by code point instead?
  2. A regular expression match knows where in the text it was found. Which property tells you?

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…