Truncate a String Bug?

Tell us what’s happening:

Not even the solution in the hints section works, nor the solutions in the forum.

Is it a bug?

Your code so far


function truncateString(str, num) {
  if (num <= 3){
    return str.slice(0, num) + "...";
  }
  else {
    return num >= str.length ? str : str.slice(0, num - 3) + "...";
  }
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; 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/

The problem is here:

str.slice(0, num - 3)

There is no need to minus 3 from num. When you make this change, you will also see that you only need a single return statement.

I also had this issue. The hint made it clear that the 3 ellipses needed to be factored into the coding. I believe that is why the solution code had the num - 3. If that is not the case, you may want to update the hints and solution in the documentation.

The current solution:

function truncateString(str, num) {
  if (str.length <= num) {
    return str;
  } else {
    return str.slice(0, num > 3 ? num - 3 : num) + '...';
  }
}