My task is to 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.
I have written the code:
function truncateString(str, num) {
if (num > str.length){
str.slice(num);
return str.append(...);
}
else if (num < 3) {
str.slice(3);
return str.append(...);
}
else {
return This is not a string;
}
}
truncateString(A-tisket a-tasket A green and yellow basket, 11);
However, it's not doing what I need it to do and returns This is not a string on every run. Can anyone help me?