[SOLVED] Truncate a string - My code is working 70% only

I can pass the four first tests with this code. I don’t know what is wrong. Maybe something with “num<=3” isn’t right?

function truncateString(str, num) {
  // Clear out that junk in your trunk
  if (str.length > num) {
  var sliced = str.slice(0, num - 3);
  return sliced + "...";
  } else if (str.length > num && num <= 3) {
    var sliced2 = str.slice(0, num);
    return sliced2 + "...";
  } else {
    return str;
  }
}
truncateString("A-tisket a-tasket A green and yellow basket", 11);

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0.

Link to the challenge:

Sorry. The solution was really simple. I didn’t need the second IF statement. I just added “num >3” to the first one.

1 Like