_:nf509453867764904a2c6488bdd1ff89bxb7
<http://schema.org/text>
"<p>Apologies. Here we go</p>\n<pre><code class=\"language-js\">export const shortenByWords = (s, n) =&gt; {\n  \nlet arr=s.split(''); // split s into array\nlet i, newArr,j,res;\nif (arr.length&gt;n){ //if s has to be shortened\n  for(i=n;i&lt;arr.length;i--){//loop through array in reverse\n    if (arr[i]==' '){\n      arr[i]='...';//change last space char to '...'\n      j=i+1;\n      break;\n    }\n  }\n  newArr=arr.slice(0,j);\n  res=newArr.join('');\n}\nelse{//is s.length is less than n\n  res=arr.join('');\n}\nreturn res;\n}</code><script async=\"\" crossorigin=\"anonymous\" data-hljs-style=\"github\" integrity=\"sha384-vbylffRV+sn4FL7ftwAw6eJ1uNcsQT3ETMpPJQZaOtzW+0d+jnlf1LJj9Jgf8Scp\" src=\"https://cdn.jsdelivr.net/gh/s9e/hljs-loader@1.0.31/loader.min.js\"></script></pre>"
<https://en.coderslang.com/d/154-task-145-shortenbywords>