Truncate a String, is a bug or I'm doing wrong?

Truncate a String, is a bug or I'm doing wrong?
0

#1

Tell us what’s happening:
I can’t fullfill the first two parameters of the challenge:
truncateString(“A-tisket a-tasket A green and yellow basket”, 8) should return “A-tisket…”. but I get ‘a-tis…’
truncateString(“Peter Piper picked a peck of pickled peppers”, 11) should return “Peter Piper…”. => “Peter Pi…” (because I did num -3 if num…)
What should I do?
I tried to do:
str.slice(0, str.length-(str.length-num)) + ‘…’;
instead of:
str.slice(0, (num - 3)) + ‘…’;
but I caught err …

Your code so far


function truncateString(str, num) {
  // Clear out that junk in your trunk
  if (str.length > num && num > 3) {
    return str.slice(0, (num - 3)) + '...';
  } else if (str.length > num && num <= 3) {
    return str.slice(0, num) + '...';
  } else {
    return str;
  }

}
truncateString("A-tisket a-tasket A green and yellow basket", 8);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/truncate-a-string


#2

If I remember correctly, you don’t need to make room for the 3 dots (num-3). If the string is bigger than the num, cut it until the num. Otherwise, return the entire string.