Truncate a String (why is the two wrong)

I think is its counting the dots Idk.

Your code so far


[spoiler]function truncateString(str, num) {
  // Clear out that junk in your trunk
  if (str.length <= num) {
    return str;
  }
  else {
    return str.slice(num) + '...';
  }
}

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



Your browser information:

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

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

@camperextraordinaire It is slicing the str by the number in the function string.
I hope it will solve it because It is removing 3 or number if it is greater than three so that it will return the slice.

Are you sure it is only that? What is the ternary operator doing?

why are you removing three? the dots? The dots don’t count in this challenge.

1 Like

changed it to 0 thanks.

If the ternary operator is still there but doesn’t do anything, maybe you actually need to remove it

1 Like

yes, just changing the ternary operator to just num will result in cleaner code.

My code is gone i passed

haha but minus 0 is not needed obviously.

make the change in the forum how every I don’t know if it runs my -0 work just fine.

Even if it pass, having unnecessary code is really bad practice.
Passing the challenge is the lesser thing, the most important thing is learning to code - learning to code means also learning to recognise if a piece of code is useless and should be removed

1 Like

What? That’s invalid code. What I meant is just num.

I not sure what you mean but i have already sliced the slice.

What I was saying is that num > 0 : num is invalid, it should be just num. That was the answer to this post, but I instead pointed you in the correct direction.