Edit: Since people seem a little confused, I will try to clarify.
Essentially, the string you take is a variable character length and can be any number of lines.
The hitch is the string has to not cutoff a word. So, as another example, the string:
'The jumping fox' is divided into 2 separate lines. The middle character is 'u', so the two lines would be: Line 1: 'The jumping' Line 2: 'Fox' to prevent the word being cutoff. So that is the two restrictions.
So, I got this question:
Given a single-line text String and a maximum line value, write the function that formats the input text , i.e: Divide the text evenly based on character, and return a new line for each section of the string. But do not cut off a line -- ie, if you have 1000 character, and 2 lines, you can increase the size of the first line to capture the end of the last word in the middle of the 500th character.
I am bashing my head trying to solve it in JavaScript. Any tips? It's easy enough to just return the lines without this whitespace formatting, but I am totally lost.
function formatString(string, numlines) {
var length = string.length;
console.log(length);
var words = string.split(' ');
var paraLength = parseInt(string.length/numlines);
console.log(paraLength)
var paragraph = [];
for(var i=0; i<length;) {
// console.log('i is: ', i);
console.log('start of string is: ', string.charAt(i));
console.log('end of string is: ', string.charAt(i+paraLength+1));
console.log('if true? ', string.indexOf(i) )
if (string.charAt(i+paraLength) == ' ') {
var thing = string.substr(i,i+paraLength);
console.log('if thing is: ', thing);
i+=paraLength;
console.log('if i is: ', i);
paragraph.push(thing);
} else {
var thing = string.substr(i,i+paraLength);
console.log('else thing is: ', thing);
console.log('blah is: ',i+paraLength+1 >= length);
while (string.charAt(i+paraLength) != ' ' && i+paraLength < length) {
thing.concat(string.charAt(i+paraLength+1));
thing.concat('hi')
console.log('else thing is: ', thing);
i+=1;
console.log('i is: ', i);
}
paragraph.push(thing);
i+= paraLength;
}
}
return paragraph.map(function(d) {
console.log('d is: ', d);
return d;
}).join('')
}
console.log(formatString('hello world blah blah blah', 2));
string.slice().nlines?'hello world blah blah blah', 2?