-1

my current code currently prints a table to the console, but I want the table to be formatted so every column and row are aligned perfectly. I can do that using an HTML table, but I would prefer just printing in the console.

console.log(multiplicationTable(12));

function multiplicationTable(max)
{
    var i,j;
    for( i = 1; i<=max ; i++)
        {
            for( j = 1; j<=max; j++)
                {
                    document.write( i*j + " " );
                }
            document.write("<br>");
        }
}

Thanks a lot!

3 Answers3

0

How about using the "console.table(~~)" ?

kyun
  • 9,710
  • 9
  • 31
  • 66
0

The trick is to use another string contain spaces to join your string.

You can have a look at this: Here

Short answer:

var str = ""; //your string
var pad = "0000"; //the buffer string
var ans = pad.substring(0, pad.length - str.length) + str //get the length of your buffer and minus your string to get the remaining '0'.
Tree Nguyen
  • 1,198
  • 1
  • 14
  • 37
0

Here is a formatted table inside your console:

console.log(multiplicationTable(12));

function multiplicationTable(max) {
  var i, j;
  document.write("<table border='1'>");
  for (i = 1; i <= max; i++) {
    document.write("<tr>");
    for (j = 1; j <= max; j++) {
      document.write("<td>" + i * j + "</td>");
    }
    document.write("</tr>");
  }
  document.write("</table>");
}
Milan Chheda
  • 8,159
  • 3
  • 20
  • 35