Skip to content

Instantly share code, notes, and snippets.

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