We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,697 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

2D array

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?

3
Contributors
2
Replies
7 Hours
Discussion Span
2 Months Ago
Last Updated
28
Views
techyworld
Junior Poster
198 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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;
}
Nomorewine
Newbie Poster
15 posts since Feb 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 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.?

DavidB
Posting Whiz
307 posts since Jul 2006
Reputation Points: 48
Solved Threads: 16
Skill Endorsements: 15

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0713 seconds using 2.67MB