ds-algo-study
Version:
Just experimenting with publishing a package
14 lines (12 loc) • 638 B
JavaScript
//Truncate a string (first argument) if it is longer than the given maximum string length (second argument). Return the truncated string with a ... ending.
function truncateString(str, num){
if(str.length > num) {
return str.slice(0,num) + "...";
}
else {
return str;
}
}
console.log(truncateString("Peter Piper picked a peck of pickled peppers",11))
console.log(truncateString("A-tisket a-tasket A green and yellow basket","A-tisket a-tasket A green and yellow basket".length,))
console.log(truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length + 2))