Skip to content

Instantly share code, notes, and snippets.

@sebshub
Created November 25, 2016 11:18
Show Gist options
  • Save sebshub/4fdc5ef7572b4398c2375b24b61e1f29 to your computer and use it in GitHub Desktop.
Save sebshub/4fdc5ef7572b4398c2375b24b61e1f29 to your computer and use it in GitHub Desktop.
Truncate a string FCC
function truncateString(str, num) {
// Clear out that junk in your trunk
if (num > 3) {
if (str.length > num) {
str = str.slice(0, num - 3) + "...";
}
return str;
} else {
str = str.slice(0, num) + "...";
}
return str;
}
/*if (str.length > num) {
if (num <= 3) {
str = str.slice(0, num);
str = str.concat("...");
return str;
} else {
str = str.slice(0, num - 3);
str = str.concat("...");
}
}
return str;
}*/
truncateString("A-tisket a-tasket A green and yellow basket", 11);
@sebshub
Copy link
Author

sebshub commented Nov 25, 2016

Truncate a string Basic Algorithm Exercise freecodecamp.com

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment