Created
December 21, 2016 12:38
-
-
Save bflannery/f28ad80c9429b1b57f2cc986c341dda5 to your computer and use it in GitHub Desktop.
Truncate A String
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 str.length > str.length num | |
// return ... | |
// if the str.length num <= 3 | |
// the dots do Not add to str.length | |
// console.log(str.length); | |
// console.log(num); | |
if (str.length > num && num > 3) { | |
return str.slice(0, (num - 3)) + '...'; | |
} else if (str.length > num && num <= 3) { | |
return str.slice(0, num) + '...'; | |
} else { | |
return str; | |
} | |
} | |
console.log(truncateString("A-tisket a-tasket A green and yellow basket", 11)); | |
console.log(truncateString("Peter Piper picked a peck of pickled peppers", 14)); | |
console.log(truncateString("A-", 1)); | |
console.log(truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length + 2)); | |
console.log(truncateString("Absolutely Longer", 2)); | |
console.log(truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment