Truncate a String (stuck)

Tell us what’s happening:

My code looks pretty much identical to the solution given in “get a hint”, I’m not sure where my mistake is?

Your code so far


function truncateString(str, num) {
  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;
  }

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/truncate-a-string

The Basic Code solution in “Get a Hint” is also wrong.

In both your code and the “Get a hint”, it’s this part here that’s causing the wrong output:

  if (str.length > num && num > 3) 
  {
    return str.slice(0, (num - 3)) + '...';
  } 

Take out the -3 from num - 3. It should only be num.

1 Like

Passing fine now. Thanks!

1 Like