Forked from anonymous/bonfire-truncate-a-string?solution=function%20truncate(str%2C%20num)%20%7B%0A%20%20%2F%2F%20Clear%20out%20that%20junk%20in%20your%20trunk%0A%20%20%2F%2FSe%20o%20str.length%20%3E%20num%20return%20%22...%22%0A%20%20%2F%2FSe%20o%20str.length%20%3C%3D%20
Last active
November 13, 2015 12:18
-
-
Save yurisbv/21589fefbadbe82b8087 to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/yurisbv 's solution for Bonfire: Truncate a string
This file contains hidden or 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: @yurisbv | |
// Challenge: http://www.freecodecamp.com/challenges/bonfire-truncate-a-string?solution=function%20truncate(str%2C%20num)%20%7B%0A%20%20%2F%2F%20Clear%20out%20that%20junk%20in%20your%20trunk%0A%20%20%2F%2FSe%20o%20str.length%20%3E%20num%20return%20%22...%22%0A%20%20%2F%2FSe%20o%20str.length%20%3C%3D%203%20retornar%20num%2B...%0A%20%20%2F*%0A%20%20if(str.length%20%3E%20num)%7B%2F%2Fadicionar%20...%0A%20%20%20%20return%20str.slice(0%2Cnum-3).concat(%22...%22)%3B%0A%20%20%20%20%20%7D%20%0A%20%20else%20%0A%20%20%20%20%20if(str.lenght%20%3C%3D%203)%7B%0A%20%20%20%20%20%20%20return%20str.slice(0%2Cnum).concat(%22...%22)%3B%0A%20%20%20%20%20%7D%0A%20%20return%20str%3B%0A%7D%0A*%2F%0A%20%20if(num%20%3C%204)%7B%0A%20%20%20%20%20%20%20return%20str.slice(0%2Cnum).concat(%22...%22)%3B%0A%20%20%20%20%20%7D%0A%20%20else%0A%20%20%20%20if%20(str.length%20%3E%20num)%7B%2F%2Fadicionar%20...%0A%20%20%20%20return%20str.slice(0%2Cnum-3).concat(%22...%22)%3B%0A%20%20%20%20%20%7D%0A%20%20%20%20else%0A%20%20%20%20%20%20return%20str%3B%0A%7D%0A%20%20%0Atruncate(%22Absolutely%20Longer%22%2C%202)%3B%0A | |
// Learn to Code at Free Code Camp (www.freecodecamp.com) | |
function truncate(str, num) { | |
// Clear out that junk in your trunk | |
//Se o str.length > num return "..." | |
//Se o str.length <= 3 retornar num+... | |
/* | |
if(str.length > num){//adicionar ... | |
return str.slice(0,num-3).concat("..."); | |
} | |
else | |
if(str.lenght <= 3){ | |
return str.slice(0,num).concat("..."); | |
} | |
return str; | |
} | |
*/ | |
if(num < 4){ | |
return str.slice(0,num).concat("..."); | |
} | |
else | |
if (str.length > num){//adicionar ... | |
return str.slice(0,num-3).concat("..."); | |
} | |
else | |
return str; | |
} | |
truncate("Absolutely Longer", 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment