Truncate a String Curiosity

Tell us what’s happening:
As always, after my solution works I go to see a hint for ways I could’ve done it better. The solution there got me a bit confused. I can’t find how is this better or more reliable than my solution and I’d just like some more insight if and why is one better than the other. Thanks

HintSolultion:

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

}

Your code so far


function truncateString(str, num) {
  // Clear out that junk in your trunk
  return str.length > num ? str.slice(0, num) + "..." : str;
}

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

Your browser information:

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

Link to the challenge:

the solution in the hint page is just wrong, as it is for a previous version of the challenge

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Thank you.

Thanks for the info! Didn’t think about it at all.