Truncate a string(here's my solution explained)

Truncate a string(here's my solution explained)


Tell us what’s happening:
I’m hoping this was a good solution. I used a ternary operator and I had looked at hundreds of solutions for ideas only to come up with one that was not very original. That being said, I believe that sharing it and explaining it is a good way to display my understanding of it. If I can explain my code, I wouldnt feel bad about its lack of originality. Moving on, my notes are in the code and my question is if these notes explain the solution well enough.

Your code so far

function truncateString(str, num) {
  // Clear out that junk in your trunk
  return str >= 3 || num <= 3 ? str.slice(0,num) + "..." : (str.length <= num ? str : str.slice(0,num - 3) + "...");
// in the ternary operator, the condition is str is greater or equal to 3 or num is less or equal to 3 and its first expression successfully truncates the first 2 strings since it slices beginning from the 0 index and ending at the num value
// the second expression is another ternary operator
// the first condition of the second ternary operator is str.length is less or equal to the num's value
// str is the first expression, which returns strings 3 and 4
// the second expression slices the str beginning at the 0 index and ending at the num value but the num value is subtracted by 3
truncateString("A-tisket a-tasket A green and yellow basket", 11);

Your browser information:

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

Link to the challenge:


Works but it’s not fail-safe
what happens when num is very large?
try for example
Just fix this part

(str.length <= num ? str : str.slice(0,num - 3) + "...")