Question: Modify the given code by: Adding a BorderedCell constructor that makes cells with a border. That is, the top of the cell has dashes (use
Modify the given code by:
Adding a BorderedCell constructor that makes cells with a border. That is, the top of the cell has dashes (use single dash, don't use underscore) across it, as well as the bottom. For the sides, use a "|". Test it by drawing a table with at least 2 rows and 2 columns with bordered text. Use functional programming (high order functions) for this.
var MOUNTAINS = [ {name: "Kilimanjaro", height: 11111}, {name: "Everest", height: 111111111} ];
function rowHeights(rows) { return rows.map(function(row) { return row.reduce(function(max, cell) { return Math.max(max, cell.minHeight()); }, 0); }); }
function colWidths(rows) { return rows[0].map(function(_, i) { return rows.reduce(function(max, row) { return Math.max(max, row[i].minWidth()); }, 0); }); } function drawTable(rows) { var heights = rowHeights(rows); var widths = colWidths(rows);
function drawLine(blocks, lineNo) { return blocks.map(function(block) { return block[lineNo]; }).join(" "); }
function drawRow(row, rowNum) { var blocks = row.map(function(cell, colNum) { return cell.draw(widths[colNum], heights[rowNum]); }); return blocks[0].map(function(_, lineNo) { return drawLine(blocks, lineNo); }).join(" "); }
return rows.map(drawRow).join(" "); }function repeat(string, times) { var result = ""; for (var i = 0; i
function TextCell(text) { this.text = text.split(" "); } TextCell.prototype.minWidth = function() { return this.text.reduce(function(width, line) { return Math.max(width, line.length); }, 0); }; TextCell.prototype.minHeight = function() { return this.text.length; }; TextCell.prototype.draw = function(width, height) { var result = []; for (var i = 0; i
function UnderlinedCell(inner) { this.inner = inner; } UnderlinedCell.prototype.minWidth = function() { return this.inner.minWidth(); }; UnderlinedCell.prototype.minHeight = function() { return this.inner.minHeight() + 1; }; UnderlinedCell.prototype.draw = function(width, height) { return this.inner.draw(width, height - 1) .concat([repeat("-", width)]); }; function dataTable(data) { var keys = Object.keys(data[0]); var headers = keys.map(function(name) { return new UnderlinedCell(new TextCell(name)); }); var body = data.map(function(row) { return keys.map(function(name) { return new TextCell(String(row[name])); }); }); return [headers].concat(body); }
console.log(drawTable(dataTable(MOUNTAINS)));
Our final result should resemble this example:

Mastro's Ocean II$$$$ I Club IS I I Denny's
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
