Skip to content

Instantly share code, notes, and snippets.

Created December 3, 2015 18:36
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/deaea178f122b4d3961e to your computer and use it in GitHub Desktop.
Save anonymous/deaea178f122b4d3961e to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/mattnwa 's solution for Bonfire: Truncate a string
// Bonfire: Truncate a string
// Author: @mattnwa
// 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
if (num <=3) {
return str.slice(0, num) + "...";
} else if(num >= str.length) {
return str;
} else {
var result = str.slice(0, num-3);
return result + "...";
}
}
truncate("A-tisket a-tasket A green and yellow basket", 11);
truncate("A-", 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment