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