JSFiddle - React, Tailwind, and code Playground

by emilcieslar

JavaScript

var w = 25;
var text = `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.`;
var output = '';
var lines = text.split(/\r|\r\n|\n/); // get number of liens
data = text.split("\n"); // split the lines into an array of lines
for (var i = 0; i < lines.length; i++) {
  var word = data[i].split(" "); // get the words of the lines

  if (w < word[0].length) // if w is less than the length of the word, we want to write the word to the output
    output += data[i].toString().substring(0, word[0].length);
  else // else write the first w characters to the output
    output += data[i].toString().substring(0, w);
}
console.log(output); // console log the output`