You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+23
Original file line number
Diff line number
Diff line change
@@ -18,6 +18,7 @@ All of these examples are from FreeCodeCamp's [course](https://www.freecodecamp.
18
18
-[5. Return Largest Numbers in Arrays Passed](#5-return-largest-numbers-in-arrays-passed)
19
19
-[6. Confirm the Ending Passed](#6-confirm-the-ending-passed)
20
20
-[7. Repeat a String Repeat a String](#7-repeat-a-string-repeat-a-string)
21
+
-[8. Truncate a String](#8-truncate-a-string)
21
22
22
23
## Basic Algorithm Scripting
23
24
@@ -261,3 +262,25 @@ function repeatStringNumTimes(str, num) {
261
262
}
262
263
}
263
264
```
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
+
functiontruncateString(str, num) {
278
+
if (str.length> num) {
279
+
returnstr.slice(0, num) +"...";
280
+
} else {
281
+
return str;
282
+
}
283
+
}
284
+
285
+
truncateString("A-tisket a-tasket A green and yellow basket", 8);
0 commit comments