Truncate a String 2 tests fail

Tell us what’s happening:

The code I came up with, which is the same as the answers given, do not pass all the tests. I can’t think of another solution to pass two of the failing tests. Even with the same exact code (both answers that are given in the hints) don’t pass the two tests. Seems like a bug to me.

Your code so far


function truncateString(str, num) {
  // Clear out that junk in your trunk
  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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36.

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

Your code returns: “A-tis…” which is only 5 characters + the eplisis. The question is asking for the first set of characters up to the maximum (set by second param) THEN add the … after.

The solution in the guide is not update-to-date with the current version of the challenge.

1 Like

I tried to update the guide but it seems to be archived? Do you know of any other link to the src?
Edit: found another repo with the correct solution but it doesnt seem to be the live to the challenge guide page? @RandellDawson

These are the two I found:

Archived: https://github.com/freeCodeCamp/guide/blob/master/src/pages/certifications/javascript-algorithms-and-data-structures/basic-algorithm-scripting/truncate-a-string/index.md

Correct but doesnt seem to show on main page?: https://github.com/freeCodeCamp/freeCodeCamp/blob/master/guide/english/certifications/javascript-algorithms-and-data-structures/basic-algorithm-scripting/truncate-a-string/index.md

Page shown: https://guide.freecodecamp.org/certifications/javascript-algorithms-and-data-structures/basic-algorithm-scripting/truncate-a-string/

The archived version is currently what shows in production. The 2nd link you reference is the current master branch which has yet to be deployed to production, but will hopefully be deployed in the near future. You will notice the 2nd link reflects the corrected version of the solution.

1 Like