Skip to content

Instantly share code, notes, and snippets.

@codebubb
Created November 14, 2015 23:05
Show Gist options
  • Save codebubb/a3ef3da0fcc3175a45e9 to your computer and use it in GitHub Desktop.
Save codebubb/a3ef3da0fcc3175a45e9 to your computer and use it in GitHub Desktop.
// Bonfire: Truncate a string
// Author: @codebubb
// Challenge: http://www.freecodecamp.com/challenges/bonfire-truncate-a-string?solution=function%20truncate(str%2C%20num)%20%7B%0A%20%20%2F%2F%20Totally%20unclear!%0A%20%20%20%20return%20str.length%20%3E%20num%20%3F%20num%20%3C%3D%203%20%3F%20str.slice(0%2C%20num)%20%2B%20%22...%22%20%3A%20str.slice(0%2C%20num%20-3)%20%2B%20%22...%22%20%3A%20str%3B%0A%7D%0A%0Atruncate(%22Absolutely%20Longer%22%2C%202)%3B%0A
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function truncate(str, num) {
// Totally unclear!
return str.length > num ? num <= 3 ? str.slice(0, num) + "..." : str.slice(0, num -3) + "..." : str;
}
truncate("Absolutely Longer", 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment