Skip to content

Instantly share code, notes, and snippets.

  • Save anonymous/f570216d124d95eeb587 to your computer and use it in GitHub Desktop.
Save anonymous/f570216d124d95eeb587 to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/sawant 's solution for Bonfire: Truncate a string
// Bonfire: Truncate a string
// Author: @sawant
// Challenge: http://www.freecodecamp.com/challenges/bonfire-truncate-a-string?solution=function%20truncate(str%2C%20num)%20%7B%0A%20%20%2F%2F%20Clear%20out%20that%20junk%20in%20your%20trunk%0A%20%20if%20(num%20%3C%20str.length)%20%7B%0A%20%20%20%20return%20(num%20%3C%3D%203%20%3F%20str.slice(0%2C%20num)%20%3A%20str.slice(0%2C%20num-3))%20%2B%20%22...%22%3B%0A%20%20%7D%20else%20%7B%0A%20%20%20%20return%20str%3B%0A%20%20%7D%0A%7D%0A%0Atruncate(%22A-tisket%20a-tasket%20A%20green%20and%20yellow%20basket%22%2C%202)%3B%0A
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function truncate(str, num) {
// Clear out that junk in your trunk
if (num < str.length) {
return (num <= 3 ? str.slice(0, num) : str.slice(0, num-3)) + "...";
} else {
return str;
}
}
truncate("A-tisket a-tasket A green and yellow basket", 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment