Javascript + Multidimensional Arrays

Associate
Joined
3 Oct 2006
Posts
2,304
Location
London
I'm currently trying to teach myself javascript and was trying to create an *interactive* quiz with javascript. You type the your answer into the boxes and the javascript checks what you have typed against the array of answers for that question. (Obviously depending on the spelling means there is "more than one answer per question").

In php i'd create a multidimensional array using the same way I've tried to below, but it doesn't work in javascript. How would I go about creating a multidimensional array like the one below in javascript?

Code:
var answers = new Array()
answers[0][0] = "traffic jam";
answers[1][0] = "choc a block";
answers[2][0] = "crawling";
answers[3][0] = "gridlock";
answers[4][0] = "bumper to bumper";
answers[5][0] = "tailback";
answers[6][0] = "bottleneck";
answers[7][0] = "nose to tail";
answers[8][0] = "stationary";
answers[9][0] = "holdups";


Thanks
 
Warning: Untested Code!

I don't think javascript supports multi dimensional arrays..? :confused:

but you can 'force' them like this:

Code:
arrayD1=10;
arrayD2=4;

myArray=new Array(arrayD1);

for(i=0;i<myArray.length;++i){
    myArray[i]=new Array(arrayD2);
}

That will make an array, with D1 being the number of 1st dimensions, D2 being the number of 2nd dimensions :)
 
Ahhh, ok thanks. I have literally just found out that this also works...

Code:
var answers = new Array(new Array(),
			new Array(),
			new Array(),
			new Array(),
			new Array(),
			new Array(),
			new Array(),
			new Array(),
			new Array(),
			new Array());

answers[0][0] = "traffic jam";
answers[1][0] = "choc a block";
answers[2][0] = "crawling";
answers[3][0] = "gridlock";
answers[4][0] = "bumper to bumper";
answers[5][0] = "tailback";
answers[6][0] = "bottleneck";
answers[7][0] = "nose to tail";
answers[8][0] = "stationary";
answers[9][0] = "holdups";
 
Yep thats basically how you do it. Here's a function which sets up and returns a multi-dimensional array.

Code:
function CreateMDArray(iRows,iCols)
{
var i;
var j;
   var a = new Array(iRows);
   for (i=0; i < iRows; i++)
   {
       a[i] = new Array(iCols);
       for (j=0; j < iCols; j++)
       {
           a[i][j] = "";
       }
   }
   return(a);
}

So to make one, you'd do:

Code:
var my_md_array=CreateMDArray(7,2);

And access it (like you did) with:

Code:
my_md_array[0][0] //my_md_array[row][column]

Jon
 
Hi,

Another way would be to define a Question class that has an "answers" attribute that is an array. You'd then hold all the answers for the question that the object represents in the array.

To represent lots of questions you'd hold an array of Question objects.

That way you don't need to use multi-dimensional arrays and you'd probably find things much easier. To make things even easier you could define a function on the question class to accept an answer and check it against all it's answers.

If you haven't got up to the object stage it may be worth having a go at this later.

Jim
 
Back
Top Bottom