Skip to content

Instantly share code, notes, and snippets.

@mtroiani
Created December 12, 2015 01:34
Show Gist options
  • Save mtroiani/b221df5e64ded9c33cc0 to your computer and use it in GitHub Desktop.
Save mtroiani/b221df5e64ded9c33cc0 to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/mtroiani 's solution for Bonfire: Truncate a String
// Bonfire: Truncate a string
// Author: @mtroiani
// Challenge: http://www.freecodecamp.com/challenges/bonfire-truncate-a-string
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function truncate(str, num) {
if (num <= 3) {
return str.slice(0,num) + "...";
}
else if (num >= str.length) {
return str;
}
else {
return str.slice(0,num - 3) + "...";
}
}
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