1.11M Members

2D array

 
0
 

Hi i want to generate a grid of random numbers and operators both vertical and horizontal using 2D array in javascript. how can i do it?

 
0
 

I didn't understand the part about operators, pardon my bad english.
but i think something like this should do it for 2d arrays of random numbers:

var x = [];
var y = [];

for(var i=0; i<10; i++)
{
    for(var m=0; m<10; m++)
        {
            y[m]= Math.random()*100; //0~100
        }
    x[i]=y;
}
 
0
 

Here is another way to create a (square) 2D array in Javascript:

var MATRIXDIM = 5;  // Dimension of a square matrix
var A = new Array(MATRIXDIM); 

for (var i = 0; i < MATRIXDIM; i++){ // Create the square Matrix
   A[i] = new Array(MATRIXDIM);
} // End for i loop

At this point, a square 5 x 5 matrix named A has been created. Now to assign data to the array, you do something like this:

A[1][2] = 25;

This assigns the number 25 to the array element in row 2, column 3 (remember, array indexing in Javascript starts at 0).

How are you getting the data in the first place? Several textboxes? A textarea box? etc.?

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article