Truncate a string Error

Can anyone help me…where i m getting wrong… i have also printed the result in console that seem good but i am unable to figure out where i am wrong… here i am posting my code:

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

truncateString("Absolutely Longer", 2);

change

 if (str.length<3) {

to

if (num <= 3) {

From the brief

I have tried that also…but it still not working…Tnks for your quick rply…bt that also not work for me…:grin:

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

truncateString("Absolutely Longer", 2);

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

truncateString("Absolutely Longer", 2);

You almost had it. Instead of str.length<=3, change it to num<=3

The instructions say:

However, if the given maximum string length num is less than or equal to 3, then the addition of the three dots does not add to the string length in determining the truncated string.

So after you check if the string length is larger than the number, you next want to check if the number is 3 or less. If so, then you slice the string to the length of the number and add 3 dots. The new string will be greater than the number, but that’s ok according to the instructions.

1 Like

Aww!! that was really a problem…thanks for helping me out!! You just give me light in the huge darkness…

It works, see my screenshot

1 Like

yeah!! but you also using num and i have used str.length for checking the condition, that was the real problem…By the way thnks for making such a good effort…