Truncate a string
EasyStrings5 min
Write a function truncate(text, maxLength). If the string is longer than maxLength, cut it and put one … character at the end, so the result is exactly maxLength long. A string that fits in maxLength comes back unchanged.
Examples
truncate('Automation testing', 10)Returns 'Automatio…'
truncate('Hello', 5)Returns 'Hello'
truncate('Hello!', 5)Returns 'Hell…'
Loading the editor...
- Input
truncate('Automation testing', 10)Expected'Automatio…' - Input
truncate('Hello', 5)Expected'Hello' - Input
truncate('Hello!', 5)Expected'Hell…'
Hidden tests run on Submit