Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

code javascript: Given an array of integers, output a string that presents the integers in tabular form. Task description You have an array of numbers

code javascript: Given an array of integers, output a string that presents the integers in tabular form.
Task description
You have an array of numbers and you would like to print these numbers in a tabular format to make them look more organized. Each cell of the table contains exactly one number and is surrounded by exactly four edges:
+-+
|4|
+-+
+-----+
|12345|
+-----+
As you can see above, each corner of the cell is represented by a "+" sign, vertical edges by "-" signs and horizontal edges by "|" signs. The width of the cell adjusts to accommodate the number of digits of the number written within it. There can be many cells in a row. Adjacent cells share an edge:
+---+---+---+---+
|4|35|80|123|
+---+---+---+---+
Note that each cell has the same width. The width of the cell adjusts to match the width of the longest number in the table. The numbers in cells are aligned to the right, with any unused area in each cell filled with spaces.
The table can consist of many rows, and adjacent rows share an edge:
+-----+-----+-----+-----+
|4|35|80|123|
+-----+-----+-----+-----+
|12345|44|8|5|
+-----+-----+-----+-----+
|24|3|22|35|
+-----+-----+-----+-----+
Your goal is to output a table containing all the numbers from a given array such that each row contains exactly K numbers. The last row can contain fewer numbers.
Write a function:
function solution(A, K);
that, given a non-empty array A consisting of N integers and an integer K, prints a string representing the formatted array. The numbers in the table should appear in the same order as the numbers in the array.
For example, given array A =[4,35,80,123,12345,44,8,5] and K =10, the resultant table will contain exactly one row, as shown below:
+-----+-----+-----+-----+-----+-----+-----+-----+
|4|35|80|123|12345|44|8|5|
+-----+-----+-----+-----+-----+-----+-----+-----+
For A =[4,35,80,123,12345,44,8,5,24,3], K =4, the table would appear as follows:
+-----+-----+-----+-----+
|4|35|80|123|
+-----+-----+-----+-----+
|12345|44|8|5|
+-----+-----+-----+-----+
|24|3|
+-----+-----+
Given A =[4,35,80,123,12345,44,8,5,24,3,22,35] and K =4, the table would appear as follows:
+-----+-----+-----+-----+
|4|35|80|123|
+-----+-----+-----+-----+
|12345|44|8|5|
+-----+-----+-----+-----+
|24|3|22|35|
+-----+-----+-----+-----+
The function shouldn't return any value.
You can print a string to the output (without or with the end-of-line character) as follows:
process.stdout.write('sample string'); process.stdout.write('whole line
');
Assume that:
N is an integer within the range [1..200];
K is an integer within the range [1..1,000,000,000];
each element of array A is an integer within the range [0..1,000,000,000].
In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

OpenStack Trove

Authors: Amrith Kumar, Douglas Shelley

1st Edition

1484212215, 9781484212219

More Books

Students also viewed these Databases questions

Question

Why is the cost of assuring dependability exponential?

Answered: 1 week ago