I am trying to create a function in javascript that will take a string of text as a paramater, and then format it for me by adding \n to the string, which will form paragraphs.
- each paragraph can only be 32 chars in length
- i have to make sure that the words are not cut in half, when going to the next paragraph"
example String:
"If we listened to our intellect, we'd never have a love affair. We'd never have a friendship. We'd never go into business, because we'd be cynical. Well, that's nonsense. You've got to jump off cliffs all the time and build your wings on the way down."
after format should be like:
If we listened to our intellect, \n
we'd never have a love affair. \n
We'd never have a friendship.\n
We'd never go into business, \n
because we'd be cynical. Well, \n
that's nonsense. You've got to \n
jump off cliffs all the time and \n
build your wings on the way down"
I am not sure how i would approach this problem, and im not expecting anyone to solve the whole solution, i just want a starting point, i am having difficulties with how to deal with spaces and words, and when to insert the \n into the string.