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):
SPOILER
(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.