Truncate a String (possible bug)

Not sure whats happening here. Had a friend even take a look at it and still can’t figure out whats going. I clicked the “get a hint” and still had the same issue. So just for kicks i plugged in one of the solutions, and the same problem persisted: the top two challenges failed.

My code so far


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;
 }

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/truncate-a-string/

In this solution you are counting the three dots in the max numbers of characters when they shouldn’t be included - I imagine this is from the Hint a section, those solutions are from a previous version of the challenge

If you post the code you tried we can see what you need to make that work

Yeah whats here is whats in the hint, so i originally had this, and the same problem came up.
function truncateString(str, num) {
if(str.length > num) {
if(num <= 3){
str = str.slice(0, num) + “…”
} else {
str = str.slice(0, num - 3) + “…”
}
}
return str;
}

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

The solution in the hint section is incorrect and has been changed, but not moved to production yet. If you click the following link, you will see what the new article will look like once it is moved to production. Be warned, the solution you see on that page will work, so do not view unless you want the solution spoiled for you.

1 Like

awesome, thank you! It looks like the solution is similar to what I originally had. Much appreciated!