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/

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.

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);

Comments

0

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

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

Comments

Your Answer

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.