Truncate a String what's the wrong

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;
  }
}

You’re subtracting 3 from the number in the slice in the first return.

EDIT link to challenge for anyone else replying: https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/truncate-a-string

1 Like

the solution in the “Get a hint” section refers to an old version of the challenge, so that will not pass

instead you have only two situations to consider:

  • the string has a length higher than the max length provided -> then you need to remove characters in eccess till the string has that length, and add three dots at the end after that
  • the string has a length equal or lower than the max length provided -> return string as it is
2 Likes