Created
September 14, 2016 01:53
-
-
Save jgresalfi/8c6ca7e7557fabb7ed21121b03071257 to your computer and use it in GitHub Desktop.
Truncate a strin...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function truncateString(str, num) { | |
if (num <= 3) { | |
str = str.slice(0, num) + "..."; | |
} else if (str.length > num) { | |
str = str.slice(0, (num - 3)) + "..."; | |
} | |
return str; | |
} | |
truncateString("Jason", 1); | |
/* Truncate a string (first argument) if it is longer than the given maximum string length (second argument). Return the truncated string with a ... ending. | |
Note that inserting the three dots to the end will add to the string length. | |
However, if the given maximum string length num is less than or equal to 3, then the addition of the three dots does not add to the string length in determining the truncated string. */ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment