Basic Algorithm Scripting: Truncate a String

Basic Algorithm Scripting: Truncate a String


Tell us what’s happening:

what am I doing wrong? I have searched everywhere and even copied and pasted some other solutions that were online and nothing worked. It keeps telling
“truncateString(“A-tisket a-tasket A green and yellow basket”, 8) should return “A-tisket…”.
truncateString(“Peter Piper picked a peck of pickled peppers”, 11) should return “Peter Piper…”.”

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/68.0.3440.106 Safari/537.36.

Link to the challenge:


Can you explain to me why you are checking if num > 3 if the first if statement?


Wouldn’t that be equal to the number of dots that we are adding “…”? I don’t know, I am confused.


This challenge is straight forward:

If the length of the string (str) is longer than the specified max (num), then truncate str so that it’s length is no longer than num and then add ‘…’ to the end. Otherwise, just return str.


Just in case others are confused by this, I do not think the solutions in the hint section are actually accurate, as I can see no reason why you would need to subtract 3 and the explanation given doesn’t make any sense to me either (which is why I’m searching the forums…glad to know I’m not crazy because I like to review the given solutions to see if I can improve mine).

Short answer, ignore the -3 and try again, I think that is incredibly misleading and doesn’t even seem to work on their own tests. Perhaps that problem used to be a slightly different problem and they have forgotten to update the solutions?

I’ll provide my solution, but make sure to understand the ternary operator defined by MDN as follows:
( condition ? expr1 : expr2):

(click below to see solution and explanation).

return str.length > num ? str.slice(0, num) + ‘…’ : str;

That should be the contents of the function. We are making use of the ternary operator to make the code clean and fit on one line (this can be used for any if statement that has only 2 possible outcomes - true or else).

We are first checking if str.length is greater than num. If it is, we have to truncate, so we use the slice method with 0 as the starting point, and the num as the cutoff point as instructed by the problem. Then we concatenate (attach) the string ‘…’ to the end, as instructed by the problem. If str.length is NOT greater than num, we simply return str. By using the return statement at the beginning, we are returning the outcome of the ternary statement, thus producing the result we want in one clean line of code.

Even if you choose not to use the ternary operator, the solution should be similar in the logic. If you are more comfortable using simpler if else statements, I don’t see anything wrong with that.

Simply put, ignore the solutions in the hint and do not concern yourself with subtracting 3. Until updated, I think that is simply inaccurate.

I hope this helps someone.


You should open an issue on Github and explain that the Guide needs to be updated.

Make sure you search for open issues which may already address this problem.