Truncate a String - Simpler Solution

Tell us what’s happening:
After solving this problem, and looking at the given solution, why was it necessary for all the conditionals? Is there something missing with my solution? Like a case?

Here is a link to the given solution.

Your code so far

[spoiler]


function truncateString(str, num) {
  const ending = "...";
  if(str.length > num) {
    let shortString = str.slice(0,num);
    console.log(shortString);
    return shortString.concat(ending);
  } else {
     return str;
  }
  console.log('OOPS');
}

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

[\spoiler]

Your browser information:

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

Link to the challenge:

The solution was written when the challenge was more complicated than it currently is.

1 Like