[SOLVED] Someone with the good eyes ? I don't see the difference in my code and Hint code, but Hint works and my not

Hello campers,
I do Truncate a string challenge here on FCC… I wrote this code:

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

It doesn’t work…
But then I copied the code from here [https://forum.freecodecamp.org/t/freecodecamp-algorithm-challenge-guide-truncate-a-string/16089](http://Truncate a String - freeCodeCampForum - Hint)… the code is:

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

}

And it works ! :DDDD… But when I look at my code and at this code from Hint, so I don’t see a difference (just a ’ ’ and the " "… but it’s not the problem). Do I need a better glases, or what’s the problem ?

Gotcha!! You have a typo in this line else if (str.lengh > num && num <= 3) it should be length and not lengh .

4 Likes

Oh, thank you very much… I do these mistakes very often, are there some ways (some web applications…) how to fast catch these mistakes ? :smiley:

It was quite easy to figure out the mistake.Your code wasnt passing for this case truncateString("A-", 1) and the only statement upon which the return value was relying was the condition.
I mostly use console.log() to check if the variale are taking up the right value;

So, I did something fairly similar, but I put the tests in a different order. It works, and it requires less typing, frankly, but I want to be sure that it’s not wrong for a specific reason.

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

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

Basically, it tests whether the truncated limit is the same or more than the length of the string /first/, so it doesn’t need to test the same thing later. After that, the only thing left to test for is whether it needs an ellipsis. I can’t think of a reason why this would cause issue - is there any reason it would?

Thanks!

You can do also like this:
function truncateString(str, num) {
if(num>=3){
if(str.length>num){
var newString=str.slice(0, num-3);
return newString.concat("…");
}
else if(str.length<=num){
return str.slice(0, num);
}
}
else if(num<3){
return str.slice(0, num).concat("…");
}
}

truncateString(“Absolutely Longer”, 2);

Try this. It’s very basic and easy to understand!

function truncateString(str, num) {
  // Clear out that junk in your trunk
  if(str.length>num)
  {
    str=str.slice(0,num)+'...';
  }
  return str;
}

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