title: How to justify text to a certain length in javascript
body: <p>I am working on a Javascript function that justifies a paragraph into a certain length (<code>function justify(str, len</code>). I have already altered the string to have each line's length be less than or equal to <code>len</code>. Now the task is to fill in enough spaces so that a line aligns with the next (pun intended) but I am having trouble. I've tried iterating through a line and adding an additional space to a character if it is a space but that did not really work. </p>
  
  <p>Consider the length to be 30:</p>
  
  <p>Before:</p>
  
  <pre><code>['Lorem ipsum dolor sit amet',
  'consectetur adipiscing elit',
  'sed do eiusmod tempor',
  'incididunt ut labore et dolore',
  'magna aliqua. Ut enim ad minim',
  'veniam, quis nostrud',
  'exercitation ullamco laboris',
  'nisi ut aliquip ex ea commodo',
  'consequat. Duis aute irure',
  'dolor in reprehenderit in',
  'voluptate velit esse cillum',
  'dolore eu fugiat nulla',
  'pariatur. Excepteur sint',
  'occaecat cupidatat non',
  'proident, sunt in culpa qui',
  'officia deserunt mollit anim']
  </code></pre>
  
  <p>After:</p>
  
  <pre><code>Lorem  ipsum  dolor  sit amet,
  consectetur  adipiscing  elit,
  sed    do    eiusmod    tempor
  incididunt ut labore et dolore
  magna aliqua. Ut enim ad minim
  veniam,      quis      nostrud
  exercitation  ullamco  laboris
  nisi  ut aliquip ex ea commodo
  consequat.   Duis  aute  irure
  dolor   in   reprehenderit  in
  voluptate  velit  esse  cillum
  dolore    eu    fugiat   nulla
  pariatur.    Excepteur    sint
  occaecat     cupidatat     non
  proident,  sunt  in  culpa qui
  officia  deserunt  mollit anim
  </code></pre>
  
  <p>Current State:</p>
  
  <pre><code>function justify(str, len) {
      let tempArray = str.split(' ').filter(word =&gt; word.length);
      //removes any empty strings resulting from splitting via the spaces
      let lines = [];
      let justified = '';
      let line = [tempArray.shift()];
  
      for (let i = 0; i &lt; tempArray.length; i++) {
          const word = tempArray[i];
  
          if ((line.join(' ') + ' ' + word).length &lt;= len) {
              line.push(word);
          } else {
              lines.push(line.join(' '));
              line = [word];
          }
      }
      /*seperates the paragraph into lines with lengths less than or equal len 
      and stores them in lines*/
  
  
      for (let i = 0; i &lt; lines.length; i++) {
          let line = lines[i].split('');
          let justifying = true;
  
          while (justifying) {
              for (let j = 0; j &lt; line.length; j++) {
                  if (line[j] === ' ') {
                      line[j] = line[j] + ' ';
                  }
  
                  if (line.length === 30) {
                      justifying = false;
                      break
                  }
  
              }
          }
  
          justified += line.join('') + '\n';
      }
      //attempt to justify the paragraph
  
      return justified;
  }
  </code></pre>