How to parse a string for having line breaking
<p>I would like to have a function which takes 3 arguments:
sentence (string), 
maxCharLen=20 (number), 
separator (string)</p>

<p>and transform the sentence based on the parameters.</p>

<p>Example</p>

<pre><code>var sentence = "JavaScript is a prototype-based scripting language that is dynamic, weakly typed and has first-class functions."

var newSentence = breakSentence(sentence, maxCharLen=20, separator="&lt;br&gt;");

newSentence // JavaScript is a prototype-based  &lt;br&gt; scripting language that is dynamic, &lt;br&gt; weakly typed and has first-class functions.
</code></pre>

<p>P.S:</p>

<p>This is what I have tried:</p>

<pre><code> var breakSentence = function (sentence, maxCharLen, separator)
 {
   sentence = sentence || "javascript is a language" ;
   maxCharLen = 10 || maxCharLen; // max numb of chars for line
   separator = "&lt;br&gt;" || separator;

   var offset;
   var nbBreak = sentence.length // maxCharLen;
   var newSentence = "";

   for (var c = 0; c &lt; nbBreak; c += 1)
   {
    offset = c * maxCharLen;
    newSentence += sentence.substring(offset, offset + maxCharLen) + separator;
   }

   return newSentence;
}
</code></pre>

<p>It works in this way:</p>

<pre><code> breakSentence()  // "javascript&lt;br&gt; is a lang&lt;br&gt;uage&lt;br&gt;"

 it should be:

 breakSentence()  // "javascript&lt;br&gt;is a &lt;br&gt;language"
</code></pre>
