Last active
April 5, 2016 02:43
-
-
Save mmloveaa/e3893c5e00f93ccfffefbcfac10f8440 to your computer and use it in GitHub Desktop.
4-4 FCC Q9 -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
| Truncate a string | |
| Truncate a string (first argument) if it is longer than the given maximum string length (second argument). Return the truncated string with a ... ending. | |
| Note that inserting the three dots to the end will add to the string length. | |
| However, if the given maximum string length num is less than or equal to 3, then the addition of the three dots does not add to the string length in determining the truncated string. | |
| Remember to use Read-Search-Ask if you get stuck. Write your own code. | |
| Here are some helpful links: | |
| String.slice() | |
| function truncateString(str, num) { | |
| // Clear out that junk in your trunk | |
| if(num<3) { | |
| return str.slice(0,num).concat("..."); | |
| } else if(str.length > num){ | |
| return str.slice(0,num-3).concat("..."); | |
| } else { | |
| return str; | |
| } | |
| } | |
| truncateString("A-tisket a-tasket A green and yellow basket", 11); | |
| // truncateString("A-tisket a-tasket A green and yellow basket", 11); | |
| // truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length) should return "A-tisket a-tasket A green and yellow basket". | |
| // truncateString("A-", 1) | |
| // should return "A...". |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment