Basic Algorithm Scripting: Truncate a String problem

Basic Algorithm Scripting: Truncate a String problem
0

#1

I’m having trouble with two of the tests that are the complete length of the string so it is not truncated.
Can anyone tell me why my code is failing the two tests?
Thanks.

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

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


#2

you only need to put the three dots in there when the string is too long


#3

This got rid of one of the two but the one that is .length +2 is failing

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

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

#4

In your earlier code, note that the condition in the if-block is exactly the opposite of the condition in the else-if-block.

num !>= str.length

You can’t put a ! before >=. The inverse of >= is just <


#5

That was it

Thank you