Skip to content

Instantly share code, notes, and snippets.

Created December 11, 2015 06:28
Show Gist options
  • Save anonymous/7003a04b2ad83b5d9bbc to your computer and use it in GitHub Desktop.
Save anonymous/7003a04b2ad83b5d9bbc 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