2021-05-05 10:13:49 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								id: 594810f028c0303b75339ad0
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								title: Align columns
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								challengeType: 5
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								forumTopicId: 302224
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								dashedName: align-columns
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# --description--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-07-16 11:03:16 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Given an array 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.
							 
						 
					
						
							
								
									
										
										
										
											2021-05-05 10:13:49 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# --instructions--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Use the following text to test your programs:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-07-16 11:03:16 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								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.'
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2021-05-05 10:13:49 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**Note that:**
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-07-16 11:03:16 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								-  The example input texts lines may, or may not, have trailing dollar characters. 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  All columns should share the same alignment. 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  Consecutive space characters produced adjacent to the end of lines are insignificant for the purposes of the task. 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  Output text will be viewed in a mono-spaced font on a plain text editor or basic terminal. Lines in it should be joined using new line character (`\n` ). 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  The minimum space between columns should be computed from the text and not hard-coded. 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  It is not a requirement to add separating characters between or around columns. 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								For example, one of the lines from the `testText` , after jusitifing to the right, left and center respectivelly:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								'    column        are separated     by     at    least       one space.\n'
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								'column     are        separated by     at     least    one       space.\n'
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								'  column      are     separated   by     at    least      one    space.\n'
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2021-05-05 10:13:49 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# --hints--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								`formatText`  should be a function. 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert(typeof formatText === 'function');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-07-16 11:03:16 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`formatText(testText, 'right')`  should produce text with columns justified to the right. 
						 
					
						
							
								
									
										
										
										
											2021-05-05 10:13:49 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2021-07-16 11:03:16 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								assert.strictEqual(formatText(_testText, 'right'), rightAligned);
							 
						 
					
						
							
								
									
										
										
										
											2021-05-05 10:13:49 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-07-16 11:03:16 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`formatText(testText, 'left')`  should produce text with columns justified to the left. 
						 
					
						
							
								
									
										
										
										
											2021-05-05 10:13:49 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2021-07-16 11:03:16 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								assert.strictEqual(formatText(_testText, 'left'), leftAligned);
							 
						 
					
						
							
								
									
										
										
										
											2021-05-05 10:13:49 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-07-16 11:03:16 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`formatText(testText, 'center')`  should produce text with columns justified to the center. 
						 
					
						
							
								
									
										
										
										
											2021-05-05 10:13:49 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2021-07-16 11:03:16 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								assert.strictEqual(formatText(_testText, 'center'), centerAligned);
							 
						 
					
						
							
								
									
										
										
										
											2021-05-05 10:13:49 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# --seed--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## --after-user-code--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2021-07-16 11:03:16 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								const _testText = [
							 
						 
					
						
							
								
									
										
										
										
											2021-05-05 10:13:49 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								  'Given$a$text$file$of$many$lines',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  'where$fields$within$a$line$',
							 
						 
					
						
							
								
									
										
										
										
											2021-07-16 11:03:16 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  'are$delineated$by$a$single$"dollar"$character',
							 
						 
					
						
							
								
									
										
										
										
											2021-05-05 10:13:49 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								  '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.'
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								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. ';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## --seed-contents--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								function formatText(input, justification) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-07-16 11:03:16 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								const testText = [
							 
						 
					
						
							
								
									
										
										
										
											2021-05-05 10:13:49 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								  '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.'
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								];
							 
						 
					
						
							
								
									
										
										
										
											2021-07-16 11:03:16 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# --solutions--
  
						 
					
						
							
								
									
										
										
										
											2021-05-05 10:13:49 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-07-16 11:03:16 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2021-05-05 10:13:49 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								String.prototype.repeat = function (n) { return new Array(1 + parseInt(n)).join(this); };
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								function formatText(input, justification) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  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') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          right += left; left = '';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        if (justification === 'right') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          left += right; right = '';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        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;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```