function tableString(arr, numCols, colSpacing = 3) {
const numRows = Math.ceil(arr.length / numCols);
const rows = Array.from(
{ length: numRows },
(n, i) => arr.slice(i * numCols, (i + 1) * numCols)
);
const widths = Array.from(
{ length: numCols },
(n, i) => Math.max(...rows.map(m => (m[i] ?? '').length))
);
return rows
.map(n => n.map((m, i) => m.padEnd(colSpacing + widths[i], ' ')).join(''))
.join('\n');
}
const arr = [
'11111', '22222222', '33333333', '444', '11', '222', '3333',
'4444444', '11111111', '222222', '33333333', '4', '1',
'2222222222', '3', '44444444444444',
];
console.log(tableString(arr, 4));
console.log(tableString(arr, 3));
console.log(tableString(arr, 3, 8));