Truncate a string hi

Tell us what’s happening:

Your code so far

function truncateString(str, num) {
  var l=str.length;
  if(l>num){
   return str.slice(0,num-3)+"...";
  }
   else if(num<=3){
      return str.slice(0,num)+"...";
    }
  else{
    return str;
  }
}

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:57.0) Gecko/20100101 Firefox/57.0.

Link to the challenge:

> Blockquotestrong text

And your question is?

The problem is with your order of conditionals

  if(l>num){
   return str.slice(0,num-3)+"...";
  }
   else if(num<=3){
      return str.slice(0,num)+"...";
    }
  else{
    return str;
  }

If you check which tests are failing, it is the bottom two tests, which test the logic of your second if statement. Your problem is that your first statement is always run before the second. You need to run your second if statement before the first. Your first if statement checks if the string length is longer than the number, which is most often true. Because of this, your second if statement which handles the scenario of the number being less than 3 is barely run. Your code in the second if statement is correct, but it is not being run, so you need to figure out a way to somehow check the number length and run that scenario before checking if the string length is greater than the number.

1 Like

thank you now i get it.

1 Like

Awesome! I’ll mark this as solved so others don’t waste time here, if you’re still struggling, don’t hesitate to ask!