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%20if(num%3Cstr.length)%20%7B%0A%20%20%20%20return%20num%3E3%20%3F%20str.slice(0%2Cnum-3).concat(%27...%27)
Created
November 30, 2015 06:17
-
-
Save srkama/15a05dde653a107fe0c0 to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/srkama '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: @srkama | |
| // 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%20if(num%3Cstr.length)%20%7B%0A%20%20%20%20return%20num%3E3%20%3F%20str.slice(0%2Cnum-3).concat(%27...%27)%3Astr.slice(0%2Cnum).concat(%27...%27)%3B%0A%20%20%7D%0A%20%20return%20str%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 | |
| if(num<str.length) { | |
| return num>3 ? str.slice(0,num-3).concat('...'):str.slice(0,num).concat('...'); | |
| } | |
| return str; | |
| } | |
| 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