Truncate a string problem

Truncate a string problem
0

#1
`    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”.


#2
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:


#3

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.


#4

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


#5

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