Rosetta Code: Align Columns Expected output

I’m trying to solve this challenge and can’t seem to make it right.
My code gives this output:

According to this site: rosettacode and considering the difference in the input I think that is the expected output, but apparently it is not.

Can anyone tell me what the expected output is so I can try to make this right?
Thanks in advance

I’m not really familiar with that task but it appears that you have lines breaking where they should not.

Your first line should contain all of these words but it does not.
Given$a$text$file$of$many$lines,$where$fields$within$a$line$

Possibly you have an issue with the comma character in some cases. Commas appear in the original string in the same places that you have unexpected breaks and those same commas are not included in your output.

That shouldn’t be the problem, since the original text in freecodecamp doesn’t include those commas. See, in fact my text does include the ones that finds (for example after Justified in the first column):
The original code from the challenge includes this:

const testArr = [
‘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.’
];

(No comma after line, except the one that separates one element from the other in the array).
I suppose the expected output is based on the given text.

Can you post the link to the challenge in free code camp? The sample input on the link you provided must be different.

Yes, that’s why I said:

:sweat_smile:

Here’s the link for the challenge:
freecodecamp Rosetta challenge

Just in case anyone wonders, I’ve finally found the expected output:

var rightAligned = ’ Given a text file of many lines\n where fields within a line \n are delineated by a single “dollar” character\n write a program\n that aligns each column of fields \n by ensuring that words in each \n column are separated by at least one space.\n Further, allow for each word in a column to be either left \njustified, right justified\n or center justified within its column.’;

var leftAligned = 'Given a text file of many lines \nwhere fields within a line \nare delineated by a single “dollar” character\nwrite a program \nthat aligns each column of fields \nby ensuring that words in each \ncolumn are separated by at least one space.\nFurther, allow for each word in a column to be either left \njustified, right justified\nor center justified within its column. ';

var centerAligned = ’ Given a text file of many lines \n where fields within a line \n are delineated by a single “dollar” character\n write a program \n that aligns each column of fields \n by ensuring that words in each \n column are separated by at least one space.\n Further, allow for each word in a column to be either left \njustified, right justified\n or center justified within its column. ';
;
The problem with my code was with the way I was handling the $ at the end of some lines (the expected output is ONE extra space.
Challenge solved!!

Hi lrvlr!
Could I please ask you to specify the requirements for the output. Below is the screenshot of my function’s return value. It doesn’t pass the test and I don’t know why.
Btw. the $ symbols at the end of the strings are converted to space character.

Thanks a lot in advance!

For what I see you are returning an array, but you are expected to return just one string, with all the text and the respective ends of lines for each line (\n).
See if you can pass the test returning a string.
If not, when you are debugging your code, at the console you can click here:


And it will take you to the source code, where at the very end you can see the expected result:

Hi!
Thanks a lot! That certainly brought me closer to the solution. However I am only passing the left aligned test. Btw. I remove the $ sign at the end of the line.
Maybe you could help me further, as after trying lot of possibilities I am still not getting there. My code is below:

function formatText (input, justification) {
var textArr = [];
var longest = []; //longest word
for(var i = 0, len = input.length; i < len; i++) {
textArr.push(input[i].split("$"));
for (var j = 0, m = textArr[i].length; j < m; j++) {
if (textArr[i][j].length > longest[j] || !longest[j]) {
longest[j] = textArr[i][j].length;
}
}
}

var spacesNr;
for (var i = 0, len = textArr.length; i < len; i++) {//iterate through each line

for (var j = 0, m = textArr[i].length; j < m; j++ ) { //iterate through each word
 var fieldLength = longest[j] + 1;
 spacesNr = fieldLength - textArr[i][j].length;
 if (j == m -1 && textArr[i][j].length == 0) {//$ at end of the line is converted to empty array...
   textArr[i].pop(); //...that should be removed
 }
 else {
  if(j == m-1) --spacesNr;
  if(justification == "center") {
    var nspc = Math.floor(spacesNr/2);
      textArr[i][j] = textArr[i][j].concat(" ".repeat(nspc));
      textArr[i][j] = (" ".repeat(spacesNr-nspc)).concat(textArr[i][j]);
  } else if (justification == "right") {
    textArr[i][j] = (" ".repeat(spacesNr)).concat(textArr[i][j]);
  } else { //left justif. as default
    textArr[i][j] = textArr[i][j].concat(" ".repeat(spacesNr));
  }
 } 

}
textArr[i] = textArr[i].join(’’);
}
textArr = textArr.join("\n");
return textArr;
}

formatText(testArr, “right”);

Thanks a lot in advance!

Well, for starters, I see you are missing spaces between columns:

The expected output is very specific, you have to return exactly the same ammount of spaces in the same places, otherwise it will not pass.
But even if it weren’t so specific, doesn’t seem very nice to print allthewordstoghether :stuck_out_tongue:

Giving a closer view, it’s the last column you are having trouble with. Look at the last two lines. The word justified is not right justified in line 9. Neither is “program” in line 4

Late to the party, but also struggling with this one.

Thanks to the OP and responses, realised that I’m supposed to be generating one ‘multi-line’ string (had started by returning an array of individual lines) and not to pad out rows with spaces to be the same length; however, I’m still not getting a ‘pass’ on the test - even for the straight-forward left-align example - and can’t spot the (probably obvious) mistake I’m making.

My output looks like this in the console:

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.

and to ease error-spotting, here’s the same text with the ‘column gaps’ turned into vertical lines rather than spaces, and a \n carriage-return visible at the end of each row to check there are no trailing spaces:

Given     |a         |text     |file  |of    |many    |lines\n
where     |fields    |within   |a     |line\n
are       |delineated|by       |a     |single|"dollar"|character\n
write     |a         |program\n
that      |aligns    |each     |column|of    |fields\n
by        |ensuring  |that     |words |in    |each\n
column    |are       |separated|by    |at    |least   |one      |space.\n
Further,  |allow     |for      |each  |word  |in      |a        |column|to|be|either|left\n
justified,|right     |justified\n
or        |center    |justified|within|its   |column.

Anyone see where I’m going wrong?!