Forked from anonymous/bonfire-truncate-a-string#.js
Created
December 7, 2015 17:43
-
-
Save robertsheacole/c6d402e0dfb27d227c5a to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/robertsheacole 's solution for Bonfire: Truncate a string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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