JSFiddle - React, Tailwind, and code Playground

by emilcieslar

JavaScript

// Text input.
var words = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book."
  .replace ( / ( ? : \r\ n | \r | \n) /g, ' ')
  .replace ( / \s\ s + /g, ' ')
  .split (' ')

var w = 25;

// Output loop.
for (var i = 0; i < words.length;) {
  var output = words[i] // Take current word.
  while (++i < words.length && output.length + words[i].length + 1 <= w)
    output += ' ' + words[i] // Take next words unless we exceed the limit.
  console.log (output) // Print the result.
}