So I just solved this algorithm…
Thanks to the error messages that pop up during testing.
This is what I don’t get:
Here are the instructions:
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.
What I don’t get is this… I solved the algorithm by adding 3 dots AFTER truncating the string in all scenarios. This means that whether the dots were included had nothing to do with it the length of the truncation. The only difference is, in the case where num was greater than 3, I did num -3 before truncating the string by num. But still, the dots were never involved in any truncation or in any actual string that was being acted upon by the slice() function, which is why the instructions make no sense to me.
See successful passing code:
if (num <= 3){
return str.slice(0, num) + '...';
}
else{
if(num >= str.length){
return str;
}
num = num -3;
return str.slice(0, num) + '...';
}