2018-10-04 13:37:37 +00:00
---
id: ac6993d51946422351508a41
title: Truncate a String
challengeType: 5
2019-07-31 18:32:23 +00:00
forumTopicId: 16089
2021-01-13 02:31:00 +00:00
dashedName: truncate-a-string
2018-10-04 13:37:37 +00:00
---
2020-11-27 18:02:05 +00:00
# --description--
2018-10-04 13:37:37 +00:00
2020-11-27 18:02:05 +00:00
Truncate a string (first argument) if it is longer than the given maximum string length (second argument). Return the truncated string with a `...` ending.
# --hints--
2021-03-03 00:12:12 +00:00
`truncateString("A-tisket a-tasket A green and yellow basket", 8)` should return the string `A-tisket...` .
2020-11-27 18:02:05 +00:00
```js
assert(
truncateString('A-tisket a-tasket A green and yellow basket', 8) ===
'A-tisket...'
);
2018-10-04 13:37:37 +00:00
```
2021-03-03 00:12:12 +00:00
`truncateString("Peter Piper picked a peck of pickled peppers", 11)` should return the string `Peter Piper...` .
2018-10-04 13:37:37 +00:00
2020-11-27 18:02:05 +00:00
```js
assert(
truncateString('Peter Piper picked a peck of pickled peppers', 11) ===
'Peter Piper...'
);
```
2018-10-04 13:37:37 +00:00
2021-03-03 00:12:12 +00:00
`truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length)` should return the string `A-tisket a-tasket A green and yellow basket` .
2018-10-04 13:37:37 +00:00
```js
2020-11-27 18:02:05 +00:00
assert(
truncateString(
'A-tisket a-tasket A green and yellow basket',
'A-tisket a-tasket A green and yellow basket'.length
) === 'A-tisket a-tasket A green and yellow basket'
);
```
2018-10-04 13:37:37 +00:00
2021-03-03 00:12:12 +00:00
`truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length + 2)` should return the string `A-tisket a-tasket A green and yellow basket` .
2020-11-27 18:02:05 +00:00
```js
assert(
truncateString(
'A-tisket a-tasket A green and yellow basket',
'A-tisket a-tasket A green and yellow basket'.length + 2
) === 'A-tisket a-tasket A green and yellow basket'
);
```
2021-03-03 00:12:12 +00:00
`truncateString("A-", 1)` should return the string `A...` .
2020-11-27 18:02:05 +00:00
```js
assert(truncateString('A-', 1) === 'A...');
2018-10-04 13:37:37 +00:00
```
2021-03-03 00:12:12 +00:00
`truncateString("Absolutely Longer", 2)` should return the string `Ab...` .
2018-10-04 13:37:37 +00:00
2020-11-27 18:02:05 +00:00
```js
assert(truncateString('Absolutely Longer', 2) === 'Ab...');
```
# --seed--
2018-10-04 13:37:37 +00:00
2020-11-27 18:02:05 +00:00
## --seed-contents--
2018-10-04 13:37:37 +00:00
2020-11-27 18:02:05 +00:00
```js
function truncateString(str, num) {
return str;
}
2018-10-04 13:37:37 +00:00
2020-11-27 18:02:05 +00:00
truncateString("A-tisket a-tasket A green and yellow basket", 8);
```
2018-10-04 13:37:37 +00:00
2020-11-27 18:02:05 +00:00
# --solutions--
2018-10-04 13:37:37 +00:00
```js
function truncateString(str, num) {
if (num >= str.length) {
return str;
}
return str.slice(0, num) + '...';
}
truncateString("A-tisket a-tasket A green and yellow basket", 8);
```