1,105,328 Community Members

2D array

Member Avatar
techyworld
Posting Whiz in Training
242 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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?

Member Avatar
Nomorewine
Newbie Poster
15 posts since Feb 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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;
}
Member Avatar
DavidB
Posting Pro in Training
433 posts since Jul 2006
Reputation Points: 44 [?]
Q&As Helped to Solve: 33 [?]
Skill Endorsements: 27 [?]
 
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 three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article