#Bonfire: Truncate a string
Explore tagged Tumblr posts
freecodecampofdespair-blog · 10 years ago
Text
Bonfire: Truncate a string
Difficulty Level: 1, codeacademy stuff, c’mon
My Solution:
function truncate(str, num) {  // Clear out that junk in your trunk  if (str.length>num) {  str = str.slice(0,num-3) + "...";  }  return str; }
Check to see that the string is, in fact, larger than the allowed number of characters. If this is the case, set str equal to a slice of itself starting at index 0 and ending at the given num minus the three character spaces reserved for the ellipses. Otherwise, return the string as you would normally.
Where’s a good retail chain to buy discount chinese-made sunglasses?
0 notes