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?
11either greater than the length of your string, or less than3?str.length < numthen it needs to truncate the string by the difference betweenstr.lengthandnumand return a truncated string with...added to the end of said string.>means "greater than" and<means "less than". Testingstr.length < numwill givetruewhen the length of the string is less than the value innum.