0

I want to create a three dimensional array in JavaScript but I'm getting an error in Chrome:

Error: Uncaught SyntaxError: Unexpected token [ 

My JavaScript looks like this:

 function ThreeDimensionalArray(iRows,iCols,iHig)
 {
 var i;
 var j;
 var z;
 var a = new Array(iRows);
 for (i=0; i < iRows; i++)
 d {
 a[i] = new Array(iCols);
 for (j=0; j < iCols; j++)
 { 
 var a[i][j] = new Array(iHig);
 for (z=0; z < iHig; z++){
 a[i][j][z] = "";
 };
 };
 };
 return(a);
 }; 
 var hello = ThreeDimensionalArray(3,3,3);

http://jsfiddle.net/JknVF/1/

mu is too short
436k71 gold badges863 silver badges822 bronze badges
asked Jun 24, 2012 at 19:17

3 Answers 3

2

Change

var a[i][j] = new Array(iHig);

to

a[i][j] = new Array(iHig);.

var indicates you want to define a new variable. a is already defined.

answered Jun 24, 2012 at 19:20
Sign up to request clarification or add additional context in comments.

2 Comments

Also this: d { and unclosed bracket for for (z=0...
@Max I think the d { was a typo since it wasn't in the fiddle.
1

Remove "var" from the following line:

var a[i][j] = new Array(iHig);
McGarnagle
103k31 gold badges235 silver badges264 bronze badges
answered Jun 24, 2012 at 19:32

Comments

0

I've corrected your code to make a working version here:

http://jsfiddle.net/JknVF/7/

answered Jun 24, 2012 at 19:36

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.