Home > OS >  Multiplication table in a console for entered number
Multiplication table in a console for entered number

Time:02-04

Good day,

I need to make a method that makes the multiplication table in console.log. This method should receive a number to which it outputs the multiplication table. The table should be appeared in the console (console.log). For example, if the number 5 came to the input, we get:

Example of table

Important note: In the last line between the numbers, exactly one space should be output. In each column, the numbers should be aligned to the right.

I have searched everywhere, but I have not found a similar solution to this particular problem anywhere. I don't quite understand how we can indent and add numbers on the sides. I only got it this way:

function multiplicationTable(value) {
    let table = '';
  
    for (let i = 1; i <= value; i  ) {
      let tableString = '';

      for (let j = 1; j <= value; j  ) {
        tableString  = ' '   (i * j)   ' ';
      }
  
      tableString  = '\n';
      table  = tableString;

    } 
  
    return table;
}

console.log(multiplicationTable(5));

CodePudding user response:

Try something like this :

function multiplicationTable(value) {
    let table = '\n';
    let maxLength = (value * value).toString().length;
  
    for (let i = 0; i <= value; i  ) {
      let tableString = '';
      
      for (let j = 0; j <= value; j  ) {
        let product = i * j;
        let padding = ' '.repeat(maxLength - product.toString().length   1);
        tableString  = padding   (product || ' ');
      }
  
      table  = tableString   '\n';
    } 
  
    console.log(table);
}

multiplicationTable(5);

Explanation :

  • let table = '\n'; creates an empty string with a newline character, which will be used to store the multiplication table.

  • let maxLength = (value * value).toString().length; finds the length of the largest number that will appear in the table, which is value * value. This length will be used to set the width of each column in the table.

  • for (let i = 0; i <= value; i ) creates a for loop that will iterate value 1 times, where i is the row number. The 0 in i = 0 is because we want the first row to display the column headers (i.e. the numbers 0, 1, 2, ..., value).

  • let tableString = ''; creates an empty string that will be used to store each row of the table.

  • for (let j = 0; j <= value; j ) creates a nested for loop that will iterate value 1 times, where j is the column number. The 0 in j = 0 is because we want the first column to display the row headers (i.e. the numbers 0, 1, 2, ..., value).

  • let product = i * j; calculates the product of the row and column numbers, which is the number that will appear in the table at that position.

  • let padding = ' '.repeat(maxLength - product.toString().length 1); adds spaces to the left of the product so that each column has the same width. maxLength is the width of each column, and product.toString().length is the length of the product. The 1 in maxLength - product.toString().length 1 adds an extra space to the left of each product.

  • tableString = padding (product || ' '); adds the padding and product (or an empty string, ' ', if i or j is 0) to the tableString. This creates the row of the table.

  • table = tableString '\n'; adds the tableString and a newline character to the table. This creates a new row in the table.

  • Related