Skip to content

Instantly share code, notes, and snippets.

Created December 3, 2015 22:16
Show Gist options
  • Save anonymous/d4aade2e70e788b2373d to your computer and use it in GitHub Desktop.
Save anonymous/d4aade2e70e788b2373d to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/subodh737 's solution for Bonfire: Truncate a string
// Bonfire: Truncate a string
// Author: @subodh737
// 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 (str.length > num) {
if (num > 3) {
str = str.slice(0, (num - 3));
} else {
str = str.slice(0, num);
}
str = str + "...";
}
//console.log(str);
return str;
}
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