Truncating a string in Javascript, confusion of last two test strings
<p>I'm working on a project for freecodecamp.</p>
<p>Rules of the project:</p>
<blockquote>
<p>Truncate a string (first argument) if it is longer than the given maximum string length (second argument). Return the truncated string with a ... ending.</p>
<p>Note that inserting the three dots to the end will add to the string length.</p>
<p>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.</p>
</blockquote>
<p>My test strings include:</p>
<blockquote>
<p><code>truncateString(&quot;A-tisket a-tasket A green and yellow basket&quot;, 11)</code> should return &quot;A-tisket...&quot;.</p>
<p><code>truncateString(&quot;Peter Piper picked a peck of pickled peppers&quot;, 14)</code> should return &quot;Peter Piper...&quot;.</p>
<p><code>truncateString(&quot;A-tisket a-tasket A green and yellow basket&quot;, &quot;A-tisket a-tasket A green and yellow basket&quot;.length)</code> should return &quot;A-tisket a-tasket A green and yellow basket&quot;.</p>
<p><code>truncateString(&quot;A-tisket a-tasket A green and yellow basket&quot;, &quot;A-tisket a-tasket A green and yellow basket&quot;.length + 2)</code> should return &quot;A-tisket a-tasket A green and yellow basket&quot;.</p>
<p><code>truncateString(&quot;A-&quot;, 1)</code> should return &quot;A...&quot;.</p>
<p><code>truncateString(&quot;Absolutely Longer&quot;, 2)</code> should return &quot;Ab...&quot;.</p>
</blockquote>
<p>Now I've gotten most of this figured out using a <code>ternary</code> operator, so it's fairly simple, except the last two <code>('A-', 1)</code> and <code>('Absolutely Longer', 2)</code>, my question being, how can I accomplish the truncating successfully and return the expected output of the last two strings?</p>
<p>Source:</p>
<pre><code>function truncateString(str, num) {
  return str.length &gt; num ? str.slice(0, num - 3) + '...' : str;
}
</code></pre>