Truncate a String kindly assist

Tell us what’s happening:

Your code so far


function truncateString(str, num) {
  // Clear out that junk in your trunk
  if (num >=str.length)return str;
  if(num <=3)return str.slice(0,num)+"...";
  
  return str.slice(0,num-3)+"...";
}

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; rv:62.0) Gecko/20100101 Firefox/62.0.

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

The first two are not checking.

@xomen85, I believe you need to return a string of num-length and then extra "..." on top of it, but not included in the num.

@xomen85,
I think @snigo is right. You are nearly there.

The first if statement returns the string unchanged if num >= str.length.

That is good as is. I would just check if num < str.length. If you do that I think you won’t need the final return statement.

Let me know if this doesn’t make sense,

Gabe

1 Like