JSFiddle - React, Tailwind, and code Playground

by emilcieslar

JavaScript

const foo = (str, length) => {
  const words = str.split(' ');
  return words.reduce((acc, current) => {
    let next = `${acc} ${current}`;
    if (next.length > length) {
      next = `${acc}\n${current}`;
    }
    return next;
  });
};

const sample = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.'

alert(foo(sample, 25))