I'm trying to accomplish the following :
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.
The code I have will only pass test if my num >2, otherwise it fails.
function truncateString(str, num) {
// Clear out that junk in your trunk
var trunString = "";
if (str.length > num) {
trunString = str.slice(0, (num -3)) + "...";
return trunString;
}
return str;
}
truncateString("A-", 1);
Found a solution for this:
var trunString = "";
if (str.length > num && num >= 4 ) {
trunString = str.slice(0, (num - 3)) + "...";
return trunString;
}
else if (str.length > num && num <= 3) {
trunString = str.slice(0, (num)) + "...";
return trunString;
}
…character itself (single character).3characters while manipulating..Like jsfiddle.net/rayon_1990/w7to3g5p(num -3)?