Photo by Jake weirick on Unsplash
Truncate a string (first argument) if it is longer than the given maximum string length (second argument). Return the truncated string with a … ending.
Provided Test Cases
- truncateString(“A-tisket a-tasket A green and yellow basket”, 8) should return “A-tisket…”.
- truncateString(“Peter Piper picked a peck of pickled peppers”, 11) should return “Peter Piper…”.
- truncateString(“A-tisket a-tasket A green and yellow basket”, “A-tisket a-tasket A green and yellow basket”.length) should return “A-tisket a-tasket A green and yellow basket”.
- truncateString(“A-tisket a-tasket A green and yellow basket”, “A-tisket a-tasket A green and yellow basket”.length + 2) should return “A-tisket a-tasket A green and yellow basket”.
- truncateString(“A-“, 1) should return “A…”.
- truncateString(“Absolutely Longer”, 2) should return “Ab…”.
Solution #1: .slice( )
Understanding the Problem: We have two inputs here:
- str: a string. It looks like the provided test cases are giving us words, letters, and characters. It doesn?t really matter. They?re all part of a singular string.
- num: a number.
Our output is going to be a string. Ultimately, we want our output to be str truncated ? meaning cut off ? after num characters with ??? added to the end of the truncated str.
So for example
truncateString(‘Aluminum’, 4)// returns ‘Alum…’
The only edge case we?re advised about is if the length of str is less than or equal to num, we need to just return str without truncating it.
Examples/Test Cases: Our provided test cases are pretty straightforward.
Data Structure: We are given strings and expected to return a string. Concatenating (adding together) strings doesn?t require us to change our data type or structure, so let?s just stick with strings.
Let?s talk about .slice() for a second:
.slice() extracts a section of a string and returns it as a new string. If you call .slice() on a string without passing it any additional information, it will return the whole string.
“Bastian”.slice()// returns “Bastian”
We have the option of passing .slice() a beginIndex and endIndex, like so
This tells .slice() where to start the slicing and where to end the slicing. Keep in mind that strings are zero-indexed! So if we wanted to return from the 2-indexed letter of ?Bastian? until but not including the 5-indexed letter of ?Bastian?, we could do this:
“Bastian”.slice(2, 5)// returns “sti”
With that in mind, we can truncate words by passing in zero as the beginIndex and num as the endIndex
“Bastian”.slice(0, 4)// returns “Bast”
- If the length of the string is less than or equal to the given number, just return the string without truncating it.
- Otherwise, truncate the string. Meaning keep the beginning of the string up until the given number and discard the rest.
- Add ??? to the end of the truncated string and return it.
Code: See below!
If you have other solutions and/or suggestions, please share in the comments!
This article is a part of the series freeCodeCamp Algorithm Scripting.
This article references freeCodeCamp Basic Algorithm Scripting: Truncate a String
You can follow me on Medium, LinkedIn, and GitHub!