JavaScript words wraping alogithm
Task: Test yourself by wrapping lines at a given number of columns without breaking words.
Solution by: @karinhan
Note: My tests in index.js
file
Source preview:
function wordWrap(str, width) {
validateWordWrapArgs(str, width);
const words = str.split(' ');
for (let wordIndex = 0; wordIndex < words.length; wordIndex++) {
const word = words[wordIndex];
const nextWord = words[wordIndex + 1];
const wordsCanFitOnSameLine = (word + nextWord).length + 1 <= width;
if (nextWord && wordsCanFitOnSameLine) {
words[wordIndex] = word + ' ' + nextWord; // merges nextWord into word
words.splice(wordIndex + 1, 1); // removes nextWord afer merging into word
--wordIndex;
}
}
return words.join('\n');
}