function printAlignedMatrix(matrix) {
const columnWidths = matrix[0].map((_, colIndex) =>
Math.max(...matrix.map(row => String(row[colIndex]).length)
));
matrix.forEach(row => {
let rowStr = '';
row.forEach((cell, colIndex) => {
const formattedCell = String(cell).padStart(columnWidths[colIndex]);
rowStr += formattedCell + ' ';
});
console.log(rowStr);
});
}
const matrix = [
[1, 12, 123],
[45, 4567, 45678],
[678, 67890, 1234567],
];
printAlignedMatrix(matrix);
/*
run:
1 12 123
45 4567 45678
678 67890 1234567
*/