Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 235a6c4

Browse files
committed
trucate a string
1 parent c46afa7 commit 235a6c4

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

‎README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ All of these examples are from FreeCodeCamp's [course](https://www.freecodecamp.
1818
- [5. Return Largest Numbers in Arrays Passed](#5-return-largest-numbers-in-arrays-passed)
1919
- [6. Confirm the Ending Passed](#6-confirm-the-ending-passed)
2020
- [7. Repeat a String Repeat a String](#7-repeat-a-string-repeat-a-string)
21+
- [8. Truncate a String](#8-truncate-a-string)
2122

2223
## Basic Algorithm Scripting
2324

@@ -261,3 +262,25 @@ function repeatStringNumTimes(str, num) {
261262
}
262263
}
263264
```
265+
266+
### 8. Truncate a String
267+
268+
_Difficulty: Beginner_
269+
270+
Truncate a string (first argument) if it is longer than the given maximum string length (second argument). Return the truncated string with a ... ending.
271+
272+
---
273+
274+
#### Solution
275+
276+
```js
277+
function truncateString(str, num) {
278+
if (str.length > num) {
279+
return str.slice(0, num) + "...";
280+
} else {
281+
return str;
282+
}
283+
}
284+
285+
truncateString("A-tisket a-tasket A green and yellow basket", 8);
286+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /