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%0A%20%20var%20short%20%3D%20str%3B%0A%20%20var%20fromEnd%20%3D%200%3B%0A%20%20%0A%20%20%2F%2Freturn%20r
Created
November 23, 2015 03:29
-
-
Save rjmccallumbigl/0c31d5d207be86213359 to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/rjmccallumbigl '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: @rjmccallumbigl | |
// 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%0A%20%20var%20short%20%3D%20str%3B%0A%20%20var%20fromEnd%20%3D%200%3B%0A%20%20%0A%20%20%2F%2Freturn%20result%20if%20shorter%0A%20%20if%20(short.length%20%3C%20num)%7B%0A%20%20%20%20%0A%20%20%20%20return%20short%3B%0A%20%20%20%20%20%20%20%20%0A%20%20%7D%0A%20%20%0A%20%20%2F%2Freturn%20result%20if%20shorter%20than%203%20characters%0A%20%20%0A%20%20if%20(short.length%20%3C%3D%203)%7B%0A%20%20%20%20%0A%20%20%20%20short%20%3D%20str.slice(0%2C%20num)%3B%0A%20%20%20%20short%20%3D%20short.concat(%22...%22)%3B%0A%20%20%20%20return%20short%3B%0A%20%20%20%20%20%20%20%20%0A%20%20%7D%0A%20%20%0A%20%20%2F%2Fshorten%20string%20to%20n%20number%20of%20characters%0A%20%20if%20(short.length%20%3E%20num)%7B%0A%20%20%20%20%0A%20%20%20%20%2F%2Ffind%20out%20if%20we%20need%20to%20add%20...%0A%20%20%20%20fromEnd%20%3D%20num%20-%20str.length%3B%0A%20%20%20%20%0A%20%20%20%20short%20%3D%20str.slice(0%2C%20fromEnd)%3B%0A%20%20%20%20%0A%20%20%20%20%2F%2Fnested%20loop%20to%20make%20sure%20...%20are%20included%20while%20it%20fits%20num%0A%20%20%20%20if%20(short.length%20%3E%3D%20num)%7B%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20if%20(num%20%3E%203)%7B%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20short%20%3D%20short.slice(0%2C%20-3)%3B%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%0A%20%20%20%20%7D%0A%20%20%20%20%0A%20%20%20%20short%20%3D%20short.concat(%22...%22)%3B%0A%20%20%20%20return%20short%3B%0A%20%20%20%20%0A%20%20%7D%0A%20%20%0A%20%20%2F%2Fmake%20sure%20to%20return%20result%20if%20it%27s%20equal%0A%20%20return%20short%3B%0A%7D%0A%0Atruncate(%22A-tisket%20a-tasket%20A%20green%20and%20yellow%20basket%22%2C%2011)%3B%0A | |
// Learn to Code at Free Code Camp (www.freecodecamp.com) | |
function truncate(str, num) { | |
// Clear out that junk in your trunk | |
var short = str; | |
var fromEnd = 0; | |
//return result if shorter | |
if (short.length < num){ | |
return short; | |
} | |
//return result if shorter than 3 characters | |
if (short.length <= 3){ | |
short = str.slice(0, num); | |
short = short.concat("..."); | |
return short; | |
} | |
//shorten string to n number of characters | |
if (short.length > num){ | |
//find out if we need to add ... | |
fromEnd = num - str.length; | |
short = str.slice(0, fromEnd); | |
//nested loop to make sure ... are included while it fits num | |
if (short.length >= num){ | |
if (num > 3){ | |
short = short.slice(0, -3); | |
} | |
} | |
short = short.concat("..."); | |
return short; | |
} | |
//make sure to return result if it's equal | |
return short; | |
} | |
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