Basic Algorithm Scripting: Truncate a String Advanced Code Solution?

Tell us what’s happening:
I was a little curious. I took a look at the advanced solution (have been for all of the basic algorithm section so far), and I found that it’s kind of wrong. The num is supposed to be the maximum string length BEFORE the …, not after. On top of that, it’s solution is a little long, when it could’ve been solved in a one-liner. Is there any way to go about changing the advanced solution or offering a correction? Is my solution actually not that optimal? Is the advanced solution correct or wrong? Please offer your opinions on the situation, it would be much appreciated.

Your code so far


function truncateString(str, num) {
  return str.length > num ? str.slice(0,num) + "..." : 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/71.0.3578.98 Safari/537.36.

1 Like

Yeah, the guide predate the current version of the challenge.

You can contribute to FreeCodeCamp as it is open source if you want

1 Like