2018-09-30 22:01:58 +00:00
---
id: 594810f028c0303b75339ad0
2020-11-27 18:02:05 +00:00
title: Align columns
2018-09-30 22:01:58 +00:00
challengeType: 5
2019-08-05 16:17:33 +00:00
forumTopicId: 302224
2021-01-13 02:31:00 +00:00
dashedName: align-columns
2018-09-30 22:01:58 +00:00
---
2020-11-27 18:02:05 +00:00
# --description--
2019-07-18 15:32:12 +00:00
2020-11-27 18:02:05 +00:00
Given a text file of many lines, where fields within a line are delineated by a single `$` 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.
2019-03-01 08:10:50 +00:00
2020-11-27 18:02:05 +00:00
# --instructions--
2019-07-18 15:32:12 +00:00
2019-02-24 10:04:29 +00:00
Use the following text to test your programs:
2020-11-27 18:02:05 +00:00
2018-09-30 22:01:58 +00:00
< pre >
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.
< / pre >
2020-11-27 18:02:05 +00:00
**Note that:**
2019-05-23 04:57:59 +00:00
< ul >
2019-03-01 08:10:50 +00:00
< li > The example input texts lines may, or may not, have trailing dollar characters.< / li >
< li > All columns should share the same alignment.< / li >
< li > Consecutive space characters produced adjacent to the end of lines are insignificant for the purposes of the task.< / li >
< li > Output text will be viewed in a mono-spaced font on a plain text editor or basic terminal.< / li >
< li > The minimum space between columns should be computed from the text and not hard-coded.< / li >
< li > It is not a requirement to add separating characters between or around columns.< / li >
2019-05-23 04:57:59 +00:00
< / ul >
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
# --hints--
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
`formatText` should be a function.
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
assert(typeof formatText === 'function');
```
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
`formatText` with the above input and "right" justification should produce the following:
2018-09-30 22:01:58 +00:00
```js
2020-11-27 18:02:05 +00:00
assert.strictEqual(formatText(testInput, 'right'), rightAligned);
```
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
`formatText` with the above input and "left" justification should produce the following:
2020-09-15 16:57:40 +00:00
2020-11-27 18:02:05 +00:00
```js
assert.strictEqual(formatText(testInput, 'left'), leftAligned);
2018-09-30 22:01:58 +00:00
```
2020-11-27 18:02:05 +00:00
`formatText` with the above input and "center" justification should produce the following:
```js
assert.strictEqual(formatText(testInput, 'center'), centerAligned);
```
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
# --seed--
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
## --after-user-code--
2018-09-30 22:01:58 +00:00
```js
2018-10-20 18:02:47 +00:00
const testInput = [
'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.'
];
2018-09-30 22:01:58 +00:00
2018-10-20 18:02:47 +00:00
const 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 \n' +
'justified, right justified\n' +
' or center justified within its column.';
const leftAligned = '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. ';
const 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 \n' +
'justified, right justified\n' +
' or center justified within its column. ';
```
2019-07-18 15:32:12 +00:00
2020-11-27 18:02:05 +00:00
## --seed-contents--
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
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.'
];
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
function formatText(input, justification) {
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
}
```
# --solutions--
2018-09-30 22:01:58 +00:00
```js
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.'
];
String.prototype.repeat = function (n) { return new Array(1 + parseInt(n)).join(this); };
2019-02-26 08:07:07 +00:00
function formatText(input, justification) {
2018-09-30 22:01:58 +00:00
let x, y, max, cols = 0, diff, left, right;
for (x = 0; x < input.length ; x + + ) {
input[x] = input[x].split('$');
if (input[x].length > cols) {
cols = input[x].length;
}
}
for (x = 0; x < cols ; x + + ) {
max = 0;
for (y = 0; y < input.length ; y + + ) {
if (input[y][x] & & max < input [ y ] [ x ] . length ) {
max = input[y][x].length;
}
}
for (y = 0; y < input.length ; y + + ) {
if (input[y][x]) {
diff = (max - input[y][x].length) / 2;
left = ' '.repeat(Math.floor(diff));
right = ' '.repeat(Math.ceil(diff));
if (justification === 'left') {
2018-10-20 18:02:47 +00:00
right += left; left = '';
2018-09-30 22:01:58 +00:00
}
if (justification === 'right') {
2018-10-20 18:02:47 +00:00
left += right; right = '';
2018-09-30 22:01:58 +00:00
}
input[y][x] = left + input[y][x] + right;
}
}
}
for (x = 0; x < input.length ; x + + ) {
input[x] = input[x].join(' ');
}
input = input.join('\n');
return input;
}
```