I have following input string
Lorem ipsum dolor sit amet consectetur adipiscing elit sed doeiusmod tempor incididunt ut Duis aute irure dolor in reprehenderit in esse cillum dolor eu fugia ...
Splitting rules by example
[
"Lorem ipsum dolor", // A: Tree words <6 letters
"sit amet", // B: Two words <6 letters if next word >6 letters
"consectetur", // C: One word >=6 letters if next word >=6 letters
"adipiscing elit", // D: Two words: first >=6, second <6 letters
"sed doeiusmod", // E: Two words: firs<6, second >=6 letters
"tempor" // rule C
"incididunt ut" // rule D
"Duis aute irure" // rule A
"dolor in" // rule B
"reprehenderit in" // rule D
"esse cillum" // rule E
"dolor eu fugia" // rule D
...
]
So as you can see string in array can have min one and max tree words. I try to do it as follows but doesn't work - how to do it?
let s="Lorem ipsum dolor sit amet consectetur adipiscing elit sed doeiusmod tempor incididunt ut Duis aute irure dolor in reprehenderit in esse cillum dolor eu fugia";
let a=[""];
s.split(' ').map(w=> {
let line=a[a.length-1];
let n= line=="" ? 0 : line.match(/ /g).length // num of words in line
if(n<3) line+=w+' ';
n++;
if(n>=3) a[a.length-1]=line
});
console.log(a);
UPDATE
Boundary conditions: if last words/word not match any rules then just add them as last array element (but two long words cannot be newer in one string)
SUMMARY AND INTERESTING CONCLUSIONS
We get 8 nice answer for this question, in some of them there was discussion about self-describing (or self-explainable) code. The self-describing code is when the person which not read the question is able to easy say what exactly code do after first look. Sadly any of answers presents such code - so this question is example which shows that self-describing is probably a myth