Truncate a String bug

Truncate a String bug
0

#1

Tell us what’s happening:
This code is exactly out of the hints and solution section buts its showing the first 2 criteria as being not met. Is this a bug?

Your code so far


function truncateString(str, num) {
  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 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36.

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


#2

It does look strange…

Considering that we are taking all the characters from first index to the index based on the passed string length minus three. I’m wondering when the author originally built that, the logic at the time was checking for string lengths which were less than 3.

When we change the first condition to only slice up until num, the tests pass:

...

if (str.length > num && num > 3) {
    return str.slice(0, (num - 3)) + '...';

...

#3

I apologize but if I use that code the first 4 conditions show not met. Would you be able to show me all of you code with an explanation if possible. I think you’re correct when it comes to the criteria of this specific algorithm.