Rosetta Code Challenges - Align columns

Tell us what’s happening:

Can you tell me if this is the correct alignment for this code . I’m confused and lost out after my code wouldn’t pass. The right , left, and center alignment is maybe not in the right text format or it could be there not aligning to justify the columns.

Your code so far

/* jshint esversion: 6 */
function formatText(input, alignment) {
  const delimiter = '$';
  
  // Split the input into rows and fields
  const rows = input.map(line => line.split(delimiter));
  
  // Find the maximum number of columns across all rows
  const maxCols = Math.max(...rows.map(cols => cols.length));
  
  // Normalize rows to have the same number of columns
  const normalizedRows = rows.map(cols => {
    while (cols.length < maxCols) {
      cols.push('');
    }
    return cols;
  });
  
  // Calculate the maximum width for each column
  const colWidths = Array.from({ length: maxCols }, (_, i) => {
    return Math.max(...normalizedRows.map(row => row[i].length));
  });

  // Debugging output for column widths
  console.log('Column Widths:', colWidths);

  // Function to align fields within their column based on alignment argument
  const alignField = (field, width, align) => {
    switch (align) {
      case 'left':
        return field.padEnd(width, ' ');
      case 'right':
        return field.padStart(width, ' ');
      case 'center':
        const totalPadding = width - field.length;
        const leftPadding = Math.floor(totalPadding / 2);
        const rightPadding = totalPadding - leftPadding;
        return ' '.repeat(leftPadding) + field + ' '.repeat(rightPadding);
    }
  };
  
  // Map through each row and align fields, then join them into a string
  const alignedRows = normalizedRows.map(row => {
    return row.map((field, idx) => alignField(field, colWidths[idx], alignment)).join(' ');
  });
  
  // Join all aligned rows with newline characters
  return alignedRows.join('\n');
}

// Sample text for testing
const testText = [
  'Given$a$text$file$of$many$lines$',
  'where$fields$within$a$line$',
  'are$delineated$by$a$single$"dollar"$character$',
  'write$a$program',
  'that$aligns$each$column$of$fields$',
  'by$ensuring$that$words$in$each$',
  'column$are$separated$by$at$least$one$space.',
  'Further,$allow$for$each$word$in$a$column$to$be$either$left$',
  'justified,$right$justified$',
  'or$center$justified$within$its$column.'
];

// Displaying aligned text for different alignments
console.log("Right Aligned:\n" + formatText(testText, 'right'));
console.log("\nLeft Aligned:\n" + formatText(testText, 'left'));
console.log("\nCenter Aligned:\n" + formatText(testText, 'center'));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0

Challenge Information:

Rosetta Code Challenges - Align columns

I had just a brief look at this, but there’s definitely some additional space at the end of each line. Take a look:

console.log(formatText(testText, 'right').split('\n')
                                         .map(l => `"${l}"`)
                                         .join('\n'))