Justify Text

Background

Aligning text in a document is one way to make it more beautiful. Documents break text into lines as soon as the maximum width of the line is reached. Some common alignments are here.

These alignments give the ability to align the text to one side of the document. But times, documents have dangling text, empty spaces at the end of the lines. This is where justify text alignment comes in.

Introduction to Justify alignment

Justify text alignment is an alignment that removes as many spaces as possible from the ends of the lines. The goal is no spaces at the end of any line, at all so that the resulting document looks as follows.

To align text like this is not as easy as it seems. There are many hurdles to making words fit in a line of a specific size. To tackle such issues, do this.

  • Adding extra spaces between words in the sentence

  • Breaking long words down: “environment” into “enviro-” and "nment"

However, using either approach carelessly leads to an ugly-looking sentence and document. To avoid that, we compute costs and use the combination with the minimum cost.

In this project, we will justify text in JavaScript to get more hands-on experience.

Problem Description

A documentation company MoogleSoft is working on new, user-requested features. One requested feature is document alignments. Engineers are working on adding alignments “Center,” “Left,” and “Right.” But the engineers can’t figure out how to let users justify text. Your job is to implement justify text features in JavaScript for this company!

Task

Use your JavaScript knowledge to help a given textual string break down into an array of lines so that each line is justified.

The above illustration shows the desired solution in a document.

⚡️CHALLENGE⚡️: Throughout this project, you can solve anything with functional programming: either using methods or recursion. Here, implement all solutions using either recursion or methods available in JavaScript. Avoid using any loops.