Skip to content

Instantly share code, notes, and snippets.

  • Save sawant/b261d0d5da7cdb78efdb to your computer and use it in GitHub Desktop.
Save sawant/b261d0d5da7cdb78efdb 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%20return%20num%20%3C%20str.length%20%3F%20str.slice(0%2C%20num%20%3C%203%20%3F%20num%20%3A%20num%20-%203)%20%2B%20%22...%22%20%3A%20str%3B%0A%7D%0A%0Atruncate(%22A-tisket%20a-tasket%20A%20green%20and%20yellow%20basket%22%2C%2011)%3B
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function truncate(str, num) {
// Clear out that junk in your trunk
return num < str.length ? str.slice(0, num < 3 ? num : num - 3) + "..." : str;
}
truncate("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