truncateString

Easy · String Manipulations · 2 test cases

Truncate a string if it is longer than the given maximum string length. Return the truncated string with a '...' ending.

Examples

truncateString('A-tisket a-tasket A green and yellow basket', 8) → 'A-tisket...'
truncateString('Peter Piper picked a peck of pickled peppers', 11) → 'Peter Piper...'

Starter code

function truncateString(str, num) {

}

Complexity of the optimal solution

Time O(k), space O(k). The `slice` method creates a new string of length `k` (where `k` is the `num` parameter). The time and space complexity are proportional to the length of the new truncated string, not the original string.

Solve truncateString in the browser