Basic Algorithm Scripting - Truncate a String

why is my codes not passing after writing all this…

Your code so far

function truncateString(str, num) {
if(str.length<=num)
{
  console.log(str);
  return str;
}
else
{
if(str.length<3 || num<3){
  str=str.slice(0,num)+"...";

}
else
{
str=str.slice(0,num-3)+'...';
}

}
}

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

Your browser information:

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

Challenge: Basic Algorithm Scripting - Truncate a String

Link to the challenge:

Running one of the failing test cases would be illustrative.

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

What do you see returned from your function in this case?


Edited for human readability:

function truncateString(str, num) {
  if (str.length <= num) {
    console.log(str);
    return str;
 } else {
    if (str.length < 3 || num < 3) {
      str = str.slice(0,num)+"...";
    } else {
      str = str.slice(0,num-3)+'...';
    }
 }
}

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.