Truncate a String - Help please

Tell us what’s happening:

I have a problem to get the third and fourth test in the challenge to work
3rd :truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length) should return “A-tisket a-tasket A green and yellow basket”.

4th: truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length + 2) should return “A-tisket a-tasket A green and yellow basket”.

Please help.

Thanks,

Your code so far


function truncateString(str, num) {
  let sub = str.substring(0, num);
  return sub + "...";
}

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

Your browser information:

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

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

Thanks a lot Randell for editing the code, I tried to put it correctly but I couldn’t and thanks for your hint, I have put the condition that you have said and it worked.

function truncateString(str, num) {
  // Clear out that junk in your trunk
   if (str.length <= num) {
     return str;
   } else if (str.length > num ) {
  let sub = str.substring(0, num);
  return sub + "...";
   }
}

You’re adding ... to the end of every string. You should only add the ellipsis at the end of the truncated string if its length is less than the original string passed in (that is, if it’s not the same as the original string).