I am creating a program that breaks a string into x characters and only in space ("").
Input paragraph:
There was no way to take a walk that day. In fact, we wandered around a leafless bush at one in the morning; but after lunch (Mrs. Reed, when there was no company, dined early), the cold winter wind brought with it clouds that were so gloomy, and rain that way
Input delimiter characters: 30
Now I get the output as follows:
var textArray = ["There was no possibility of ta","king a walk that day. We had b","een wandering, indeed, in the ","leafless shrubbery an hour in ","the morning; but since dinner ","(Mrs. Reed, when there was no ","company, dined early) the cold"," winter wind had brought with ","it clouds so sombre, and a rai","n so "]`
But I only want splits in space (). It is broken in the last space before the specified number of characters.
I need the output as follows:
var textArray = ["There was no possibility of", "taking a walk that day. We", "had been wandering, indeed, in", "the leafless shrubbery an hour", "in the morning; but since", "dinner (Mrs. Reed, when there", "was no company, dined early)", "the cold winter wind had", "brought with it clouds so", "sombre, and a rain so", "penetrating, that further", "out-door exercise was now out", "of the question."]`
I tried this code:
function textToArray() {
var str = document.getElementById('stringg').value;
var strArr = [];
var count = parseInt(document.getElementById('numberOfWords').value, 10) || 1;
for (var i = 0; i < str.length; i = i + count) {
var j = i + count;
strArr.push(str.substring(i,j));
}
}