Skip to content

Instantly share code, notes, and snippets.

Created December 9, 2015 07:18
Show Gist options
  • Save anonymous/f19e9d5f106590b29f32 to your computer and use it in GitHub Desktop.
Save anonymous/f19e9d5f106590b29f32 to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/kkas 's solution for Bonfire: Truncate a string
// Bonfire: Truncate a string
// Author: @kkas
// Challenge: http://www.freecodecamp.com/challenges/bonfire-truncate-a-string
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function truncate(str, num) {
// Clear out that junk in your trunk
var len = num > 3 ? num - 3 : num;
return str.length > num ? str.slice(0, len) + "..." : str;
}
//truncate("A-tisket a-tasket A green and yellow basket", 11);
//truncate("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length);
truncate("A-", 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment