Truncate a string algo

Tell us what’s happening:
im getting the correct output for all test cases if i just console log it
but the 3rd and 4th are not clearing even tho im getting the correct answer.

Your code so far


function truncateString(str, num) {
    if(str.length <= num){
      console.log(str)
      return str + ' .'
    
    }else{
    let ans = str.slice(0,num) ;
 ans +="...";
return ans;}
}
console.log(truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length+2 ) ); 
//"A-tisket a-tasket A green and yellow basket".

Your browser information:

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

Challenge: Truncate a String

Link to the challenge:

@hussamkhatib For the following test case:

truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length)

your function is returning the following string:
"A-tisket a-tasket A green and yellow basket ."
instead of the correct string:
"A-tisket a-tasket A green and yellow basket"

It is a subtle difference, but it is a difference.

For the following test case:

truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length + 2)

your function is returning the following string:
"A-tisket a-tasket A green and yellow basket . "
instead of the correct string:
"A-tisket a-tasket A green and yellow basket"

1 Like

thanks that worked out.