Created
November 25, 2016 11:18
-
-
Save sebshub/4fdc5ef7572b4398c2375b24b61e1f29 to your computer and use it in GitHub Desktop.
Truncate a string FCC
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) { | |
// Clear out that junk in your trunk | |
if (num > 3) { | |
if (str.length > num) { | |
str = str.slice(0, num - 3) + "..."; | |
} | |
return str; | |
} else { | |
str = str.slice(0, num) + "..."; | |
} | |
return str; | |
} | |
/*if (str.length > num) { | |
if (num <= 3) { | |
str = str.slice(0, num); | |
str = str.concat("..."); | |
return str; | |
} else { | |
str = str.slice(0, num - 3); | |
str = str.concat("..."); | |
} | |
} | |
return str; | |
}*/ | |
truncateString("A-tisket a-tasket A green and yellow basket", 11); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Truncate a string Basic Algorithm Exercise freecodecamp.com
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.