Truncate a String - All tests but one

Hey y’all, I know there are a few posts on this one, but I just want to know why this particular solution does not pass the test.

Thanks!


function truncateString(str, num) {
  // Clear out that junk in your trunk

  let truncString = '';
  let target = str.length - (num + 3);

  if(str.length < num) {
    truncString = str + '...'
  }else if(num >= str.length){
    truncString = str;
  }else {
    truncString = str.slice(0, num) + '...'
  }
  console.log(truncString);
  return truncString;
}


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.169 Safari/537.36.

Link to the challenge:

1 Like

Hint:
Look at your first if statement & look at the test you’re failing.

Bigger hint:

Example:
(‘four’, 6) passes your if statement
(‘four…’, 6) does not., because ‘four…’ is now 7 characters long.

1 Like

You don’t need to include the dots in the length of the string. - but target is not used anywhere so it doesn’t matter

If str.length < num is true (first if statement) you don’t need to add dots to it: if your string is “A” and the max length is 6 it shouldn’t return A...
Also the second if statement is executed only when num === str.length because the first if statement is executed for num > str.length

2 Likes

Got it. Thanks for your time.