Am stuck here guys, help out please

Tell us what’s happening:

hello fellow coders, i dont know whats wrong with this. i tried all i can but it kept failing the test 1 and 2 (note: it passed others just the 1 & 2) please help out.

Your code so far


function truncateString(str, num) {
  if (str.length <= num) {
    return str;
  } else {
    return str.slice(0, num > 3 ? num - 3 : num) + '...';
  }
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36.

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

why all that for the second argument of slice?

It appears to be a copy/paste from the guide’s Advanced Solution which is not up-to-date.

@Dereje1 it works, thanks

@RandellDawson yes, but i ve learnt my lesson of frustration. thanks

I think it maybe a carryover from the old curriculum, I vaguely remember a modified version of this problem with length 3 as some type of constraint or another.

@p-gold If your question is how that solution worked or is supposed to work, I can understand, but if you are just copying it to pass the challenge, I encourage you to better understand how .slice() works for strings and ask for elaborations from the forum if need be, otherwise you will only be robbing your self of the opportunity to learn, which will most definitely come back to bite you later on down the line.

Here’s my only advice about .slice().

Never use it.

Haha just kidding. It’s probably one of the most used methods I use. Definitely learn it.