JSFiddle - React, Tailwind, and code Playground

by emilcieslar

JavaScript

const outPutWords1 = (w, text) => {
  let lines = text.split("\n");
  let words = [];
  let textToReturn = "";
  for (line of lines) {
    words = line.split(" ");
    for (wIndex in words) {
      if (words[wIndex].length > w) {
        words.splice(wIndex, 1);
      }
    }
    textToReturn += words.join(" ");
    textToReturn += "\n";
  }

  return textToReturn;
}

const outPutWords2 = (w, text) => {
  let lines = text.split("\n");
  let textToReturn = "";
  for (line of lines) {
    var regex = '/^(\w{0,' + w + '})$/';

    line.replace(regex, "");
    textToReturn += lines.join("\n");
  }

  return textToReturn;
}

const 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. 
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.`;

const text2 = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.\n' +
  "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.\n" +
  'It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.';

const final = outPutWords1(25, text2);
const final2 = outPutWords2(25, text2);
debugger