Rosetta Code Challenges - Align columns - failing tests 2, 3 and 4

Tell us what’s happening:

I’m failing tests 2, 3 and 4 in “Align Columns” Rosetta Code challenge, although my output seems correct.

The tests are:

  • Failed:formatText(testText, 'right') should produce text with columns justified to the right.

  • Failed:formatText(testText, 'left') should produce text with columns justified to the left.

  • Failed:formatText(testText, 'center') should produce text with columns justified to the center.

And the errors say:

formatText(testText, 'right')

should produce text with columns justified to the right.

formatText(testText, 'left')

should produce text with columns justified to the left.

formatText(testText, 'center')

should produce text with columns justified to the center.

I’ve checked the spacing in the challenge example, and it seems to match my output exactly, but I can’t seem to get the tests to pass. Has anyone encountered similar or know where I could find more information on the testing?
I’ve included my code and a link to the challenge.
Many thanks

Your code so far

function formatText(input, justification) {
  let numCols = 0;
  //split words and push each row to inputArray 
  let inputArray = []
  input.map((row) => {
    let regex = new RegExp(/[$]+$/)//I've tried removing these 2 rows
    row = row.replace(regex,'')//see line above
    row = row.split('$')
    if (row.length > numCols) {
      numCols = row.length-1
    }
    inputArray.push(row)
  })

   //push empty field to rows to make all rows the same length
  inputArray.map((row) => {
    while (row.length < numCols+1) {
      row.push('')
    }
  })

//calculate column widths based on longest word at that column index
let colWidths = []
for (let col = 0; col < numCols ; col++){
  let colWidth = 0
  for (let rowIndex = 0; rowIndex < inputArray.length; rowIndex ++ ) {
    if (inputArray[rowIndex][col].length > colWidth) {
      colWidth = inputArray[rowIndex][col].length
    }
  }
  colWidths.push(colWidth)
  colWidth = 0
}

   //create function to pad words
   function addSpaces(word, justification, width) {
  switch(justification) {
     case 'left':
        return word.padEnd(width, ' ');
     case 'right':
        return word.padStart(width, ' ');
     case 'center':
        const padding = width-word.length
        const leftPad = Math.floor(padding/2)
        return ' '.repeat(leftPad)+word+' '.repeat(padding-leftPad);  
     }
}

   //iterate over inputArray array, and for each word, add spacing accordingly, joining words in the row with one space
let result = inputArray.map((row, rowIndex) => {
return row.map((word, wordIndex) => 
addSpaces(word, justification, colWidths[wordIndex])).join(' ')
  })

result = result.join('\n')
   return result
}




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.'
];






Your browser information:

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

Challenge Information:

Rosetta Code Challenges - Align columns

It looks like lines with less words than the longest line, are still being filled up with spaces, after the last aligned word of that line.

Hi sanity, thanks for taking a look, I appreciate it.
I had added empty elements at the end of the shorter rows, to make all rows the same length. I’ve messed about with it on codepen to remove this part of the code, but it still doesn’t seem to pass the tests.

https://codepen.io/miriambyrne/pen/OJqMywX

This is my edited code:

function formatText(input, justification) {
   let numCols = 0;
   //split words and push to inputArray in rows
   let inputArray = []
   input.map((row) => {
      let regex = new RegExp(/[$]+$/)
      row = row.replace(regex,'')
      row = row.split('$')
      if (row.length > numCols) {
         numCols = row.length-1
      }
      inputArray.push(row)
   })

   //calculate column widths based on longest word at that column index
   let colWidths = []
   for (let col = 0; col < numCols ; col++){
      let colWidth = 0
      for (let rowIndex = 0; rowIndex < inputArray.length; rowIndex ++ ) {
         if (inputArray[rowIndex][col]) {
         if (inputArray[rowIndex][col].length > colWidth) {
            colWidth = inputArray[rowIndex][col].length
         }
         }
      }
      colWidths.push(colWidth)
      colWidth = 0
   }

   //create function to pad words
   function addSpaces(word, justification, width) {
      switch(justification) {
         case 'left':
            return word.padEnd(width, ' ');
         case 'right':
            return word.padStart(width, ' ');
         case 'center':
            const padding = width-word.length
            const leftPad = Math.floor(padding/2)
            return ' '.repeat(leftPad)+word+' '.repeat(padding-leftPad);  
      }
   }

   //iterate over inputArray array again, and for each word, add spacing accordingly
   let result = inputArray.map((row, rowIndex) => {
      return row.map((word, wordIndex) => 
                     addSpaces(word, justification, colWidths[wordIndex])).join(' ')
   })

   result = result.join('\n')
   return result
}




const testText = [
   'Given$a$text$file$of$many$rows',
   'where$fields$within$a$row$',
   'are$derowated$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.'
];
console.log(formatText(testText, 'left'))





Hmm, so it seems when line ends with $ it still needs additional separator, as if there is another word there. But that blank word should not be included (or aligned).

Hi sanity, that was so helpful, thank you! I didn’t account for the tests looking for an empty field, for the lines ending in $. Tests have now finally passed :grinning: