Regular Expressions - Positive and Negative Lookahead

What is your hint or solution suggestion?
I try this:

 function formatText(input, justification) {
    let formattedText = "";
    let columnWidths = [];
  
    const lines = input.split("\n");

    // Calculate the maximum width of each column
    for (let line of lines) {
        const fields = line.split("$");
        for (let i = 0; i < fields.length; i++) {
            const field = fields[i];
            if (!columnWidths[i] || field.length > columnWidths[i]) {
                columnWidths[i] = field.length;
            }
        }
    }

    for (let line of lines) {
        const fields = line.split("$");
        for (let i = 0; i < fields.length; i++) {
            const field = fields[i];
            const justifiedField = justify(field, justification[i], columnWidths[i]);
            formattedText += justifiedField;
            if (i < fields.length - 1) {
                formattedText += " ";
            }
        }
        formattedText += "\n";
    }
    return formattedText;

    function justify(field, justification, width) {
        switch (justification) {
            case "left":
                return field.padEnd(width, " ");
            case "right":
                return field.padStart(width, " ");
            case "center":
                const padding = (width - field.length) / 2;
                const leftPadding = Math.floor(padding);
                const rightPadding = Math.ceil(padding);
                return " ".repeat(leftPadding) + field + " ".repeat(rightPadding);
            default:
                return field.padEnd(width, " ");
        }
    }
}

But it didn´t workout. I´m still tryng to figereout and solve this challenge.

Challenge: Align columns

Link to the challenge:

https://www.freecodecamp.org/learn/coding-interview-prep/rosetta-code/align-columns

Welcome to the forum @williampeterson755

The error message is saying that the return statement is outside of a function.

The second error is that input is not defined.

Is this the full code?

Happy codnig

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.