Home > Mobile >  Javascript Multiplication table in 2D array
Javascript Multiplication table in 2D array

Time:05-17

I've a task to write a program that creates a multiplication table for the given variable n. The results need to be saved to a two-dimensional array. In the console I need to display the entire table with appropriate data formatting (as below). I'm starting with Javascript and already know loops and arrays only, I haven't learnt functions yet so I need some really basic solution. This is how the result should look like:

enter image description here

Here is my code that I wrote so far and I don't know what to do next:

const n = 3;
const calc = []

for (let i = 1; i <= n; i  ) {
    for (let j = 1; j <= n; j  ) {
        calc.push(i   " * "   j   " = "   (i * j));
    }
    console.log(calc)
}

CodePudding user response:

From the code you've presented, it appears that you're yet to understand what a 2d array really is. The way you've done it, everything's just stuffed into a single 1d array.

You may think of the pixels on a screen as being part of a 2d array. The first array holds all of the horizontal lines. The horizontal lines contain the pixels.

So.. let's concentrate on that part first.

let result = [];

for (var y=0; y<n; y  )
{
    result.push( [] );
    for (var x=0; x<n; x  )
    {
        result[y].push( (x 1) * (y 1) );
    }
}

First we create a result array, and then for every row in our table, we add an array that will hold the columns, then for each column in that row, we add the values.

Next, we just step through that table and turn the values into the original equations. We start by ascertaining the dimensions of the array, before we create a table the same size.

function printTable( tbl )
{
    let nRows = tbl.length;
    let nCols = tbl[0].length;
    
    for (var y=0; y<nRows; y  )
    {
        let rowStr = '';
        
        for (var x=0; x<nCols; x  )
        {
            if (x!=0) rowStr  = " | ";
            rowStr  = `${y 1} x ${x 1} = ${tbl[y][x]}`;
        }
        console.log(rowStr);
    }
}

Chucking it all together, I present the following code. Yes, I know you're not using functions yet - but you can still see the important concepts. You can change it so that the whole equation for each entry in the table is saved, instead of just the answer.

window.addEventListener('load', init, false);

function init()
{
    let result = makeMultTable(3);
    printTable(result);
}

function makeMultTable(n)
{
    let result = [];
    
    for (var y=0; y<n; y  )
    {
        result.push( [] );
        for (var x=0; x<n; x  )
        {
            result[y].push( (x 1) * (y 1) );
        }
    }
    return result;
}

function printTable( tbl )
{
    let nRows = tbl.length;
    let nCols = tbl[0].length;
    
    for (var y=0; y<nRows; y  )
    {
        let rowStr = '';
        
        for (var x=0; x<nCols; x  )
        {
            if (x!=0) rowStr  = " | ";
            rowStr  = `${y 1} x ${x 1} = ${tbl[y][x]}`;
        }
        console.log(rowStr);
    }
}

CodePudding user response:

Multiplication of table in two dimension array

Function name: multiplyNumber

Syntex multiplyNumber(n)

  • n Number to be passed to function to create table upto that number.

Snippet

function multiplyNumber(n){
        let table  = [];
        let data = [];
        for (var i = 1; i <= n; i  ){
                for (var j = 1; j <= n; j  ){
                        data.push(`${i} * ${j} = ${i * j}`);
                }
        table.push(data); // pushing data to table.
        data = []; //Resetting data array to store new data each time.
        }
        return table;
}

var result = multiplyNumber(3);
console.log(result);

CodePudding user response:

You need to reset calc for every line. I moved calc's variable declaration into the first loop.

const n = 3;

for (let i = 1; i <= n; i  ) {
    const calc = [];
    for (let j = 1; j <= n; j  ) {
        calc.push(i   " * "   j   " = "   (i * j));
    }
    console.log(calc)
}
  • Related