Skip to content

Instantly share code, notes, and snippets.

Created December 4, 2015 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/77f7e8dc7343d225262d to your computer and use it in GitHub Desktop.
Save anonymous/77f7e8dc7343d225262d to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/dbess1 's solution for Bonfire: Truncate a string
// Bonfire: Truncate a string
// Author: @dbess1
// 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(str.length > num && num > 3){
return str.slice(0, num - 3) + "...";
}
else if(str.length <= num){
return str;
}
else if(num <= 3){
return (str.slice(0, num) + "...");
}
}
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-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length + 2);
truncate("A-", 1);
truncate("Absolutely Longer", 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment