Write truncate(text, maxLength). If text has at most maxLength characters, return it unchanged. Otherwise return a shorter version that ends with … (one character, U+2026) and is at most maxLength characters long, the … included.
Cut between words, never inside one. Words are separated by whitespace: spaces, tabs, and line breaks. Punctuation belongs to the word it touches, so Hello, is one word.
Keep the start of text, exactly as written, up to the end of the last whole word that fits in the first maxLength - 1 characters, and add the …. Whitespace between the kept words stays as it is (a tab stays a tab); whitespace after the last kept word is dropped.
If not even the first word fits, cut it instead: keep the first maxLength - 1 characters and add the …. Characters are counted the way length counts them, and maxLength is at least 1.
truncate("The quick brown fox", 12); // "The quick…"
truncate("The quick brown fox", 16); // "The quick brown…"
truncate("Hello, world", 8); // "Hello,…"
truncate("Supercalifragilistic", 6); // "Super…"
truncate("Short", 10); // "Short"
expect(truncate("Hello world", 11)).toBe("Hello world");
expect(truncate("Hi", 10)).toBe("Hi");
expect(truncate("", 3)).toBe("");
expect(truncate("The quick brown fox", 12)).toBe("The quick…");
expect(truncate("The quick brown fox", 18)).toBe("The quick brown…");
expect(truncate("The quick brown fox", 10)).toBe("The quick…");
expect(truncate("The quick brown fox", 16)).toBe("The quick brown…");
expect(truncate("The quick brown fox", 15)).toBe("The quick…");
const text = "The quick brown fox jumps over the lazy dog";
for (let max = 1; max < text.length; max++) {
const result = truncate(text, max);
expect(result.length).toBeLessThanOrEqual(max);
expect(result.endsWith("…")).toBe(true);
}
expect(truncate("one two\nthree four", 12)).toBe("one two…");
expect(truncate("one\ttwo three", 8)).toBe("one\ttwo…");
expect(truncate("one two three", 8)).toBe("one…");
expect(truncate("one two \n three", 10)).toBe("one two…");
expect(truncate("Hello, world", 8)).toBe("Hello,…");
expect(truncate("Wait... what?", 9)).toBe("Wait...…");
expect(truncate("Supercalifragilistic", 6)).toBe("Super…");
expect(truncate("Supercalifragilistic is long", 10)).toBe("Supercali…");
expect(truncate("abc", 1)).toBe("…");
Run tests runs every test on your code, here in your browser. The problem is solved when they all pass.
Ctrl + Enter runs the tests (⌘ + Enter on a Mac).
I'm here to help with Truncate at a word boundary. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.