Last active
December 20, 2015 06:01
-
-
Save rurtubia/4ae20c531e23a1aa2df1 to your computer and use it in GitHub Desktop.
My solution to FreeCodeCamp bonfire 11
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 the three dots at the end add to the string length. If the num is less than or equal to 3, then the length of the three dots is not added to t…
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 truncate(str, num) { | |
// Clear out that junk in your trunk | |
//If the num is less than or equal to 3, then the length of the three dots is not added to the string length. | |
if (num <= 3) | |
return str.slice(str, num) + "..."; | |
//Truncate a string if it is longer than the given maximum string length | |
else if(str.length > num) | |
return str.slice(str, num-3) + "..."; | |
else if (str.length <= num) | |
return str; | |
} | |
truncate("A-", 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment