Truncate a string problem

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

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

Here is my code. Whether my approach is correct or not?
I’m getting these errors.

  • truncateString(“A-tisket a-tasket A green and yellow basket”, “A-tisket a-tasket A green and yellow basket”.length) should return “A-tisket a-tasket A green and yellow basket”.

  • truncateString(“A-tisket a-tasket A green and yellow basket”, “A-tisket a-tasket A green and yellow basket”.length + 2) should return “A-tisket a-tasket A green and yellow basket”.

truncateString("A-tisket a-tasket A green and yellow basket", 11);`   <---------- You see this little ' at the end ?   Get rid of that and you should be fine.

:slight_smile:

Note that you’ll never reach the third condition. num > 3 and num <= 3 are “complementary” (if the one of them is true, then the other one is false).

Consider that final conditional block as a special case, and special cases typically go at the start and have their own if-blocks.

’ was not there in the code. It was added while creating thread here. Thanks for the help.

I removed third condition and made it a while loop at the beginning. Thanks :slight_smile: