Truncate a String (regarding the number 3)

Tell us what’s happening:
I was able to come out with the solution. However i could not understand the usage of the number 3 which everyone is using in the official thread below.

https://forum.freecodecamp.org/t/freecodecamp-algorithm-challenge-guide-truncate-a-string/16089

Am i missing something or is there a problem with my solution?

Thanks in advance.

Your code so far


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

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

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

1 Like

When the curriculum was updated recently, the challenge changed the requirements. The newer challenge is less complicated.

See below for the older version of the challenge instructions:

Truncate a string (first argument) if it is longer than the given maximum string length
(second argument). Return the truncated string with a … ending.

Note that inserting the three dots to the end will add to the string length.

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.

3 Likes