Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save robertsheacole/c6d402e0dfb27d227c5a to your computer and use it in GitHub Desktop.
Save robertsheacole/c6d402e0dfb27d227c5a to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/robertsheacole 's solution for Bonfire: Truncate a string
// Bonfire: Truncate a string
// Author: @robertsheacole
// 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 dots = "...";
var empty = "";
if (str.length <= 2) {
num = num - 1;
return str.slice(num, -1) + dots;
} else if (str.length > num && num > 3) {
num = num - 3;
return str.slice(0, num) + dots;
} else if (str.length == num && num > 3){
return str;
} else if (str.length < num && num > 3) {
return str;
} else if (num <= 2){
return str.slice(0, num) + dots;
} else {
return empty;
}
}
console.log(truncate("A-", 1));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment