0
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title></title>
  <script type="text/javascript">
     function arr(){
     var a=new Array(new Array());
     var x;
     for(i=0;i<2;i++)
     {
         for(j=0;j<2;j++)
         {
             x=prompt("Enter an element for a["+i+"]["+j+"]"," ");
             a[i][j]=x;
         }
     }

     for(i=0;i<2;i++)
     {
         for(j=0;j<2;j++)
         {
            document.write(a[i][j]);
         }
     }

     document.close();

     }


     </script>

</head>

<body onLoad="arr();">

</body>
</html>

The code above was tested on Firefox. Only three prompts are displayed, instead of four:

a[0][0]
a[0][1]
a[1][0]

The array is also not printed. What's my mistake? How to fix this?

bfavaretto
  • 71,580
  • 16
  • 111
  • 150
violet kiwi
  • 695
  • 2
  • 10
  • 19

3 Answers3

1

Really sorry for the answer before, this is the correct answer

    <!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title></title>
  <script type="text/javascript">
     function arr()
     {
     var a = new Array();
     var x;

 for(i=0;i<2;i++)
 {
     a[i] = new Array();
     for(j=0;j<2;j++)
     {
         x="";
         x = prompt("Enter an element for a["+i+"]["+j+"]","");
     }
 }

 for(i=0;i<2;i++)
 {
     for(j=0;j<2;j++)
     {
        document.write(a[i][j]);
     }
 }

 document.close();

 }


 </script>

the problem is you must declare new array inside the 1st array example : How can I create a two dimensional array in JavaScript?

Community
  • 1
  • 1
goravine
  • 266
  • 3
  • 11
1

You are declaring one array less than you should. For this example, you need an array like [[],[]]. I'd code it like this (to allow for any max value of i and j):

function arr(){
     var a = [];
     var x;
     for(i=0;i<2;i++){
         a[i] = [];
         for(j=0;j<2;j++){
             x=prompt("Enter an element for a["+i+"]["+j+"]"," ");
             a[i][j]=x;
         }
     }

     for(i=0;i<2;i++){
         for(j=0;j<2;j++){
            document.write(a[i][j]);
         }
     }

}

The reason it was also not printing is that the missing array was causing an error, interrupting the execution of your function.

bfavaretto
  • 71,580
  • 16
  • 111
  • 150
  • Thankyou! why new Array(new Array()) is not working?its equivalent to a[][].right? does java script support var a=[][]? – violet kiwi Feb 07 '13 at 02:50
  • No, it's equivalent to `[[]]`. I guess you're coming from Java or C#? `new Array(new Array())` is not an "array of arrays", it's an array object containing a single array inside. And yes, javascript supports array literals like `[]`, and object literals like `{foo : 'bar'}`. It's often more practical to use them instead of the `Array()` and `Object()` constructors. – bfavaretto Feb 07 '13 at 02:50
  • fine. iam coming from C and C++ background.i want to acheive a[2][2] in javascript.var a=[2][2] is correct? – violet kiwi Feb 07 '13 at 02:59
  • In JavaScript you don't declare the array sizes. You either declare the whole thing as a literal (`var a = [[],[]]`), or create the nested arrays (`a[0]` and `a[1]`) inside the loop (like my example does for `a[0]..a[i]`). – bfavaretto Feb 07 '13 at 03:03
  • 1
    This would work too, but I don't like how it looks: `var a = new Array(new Array(2), new Array(2))`. If you pass an int to the `Array` constructor, if will initialize an array with that length, filled with undefined values. If you pass anything else (or any list of elements), you're filling the array with those elements. – bfavaretto Feb 07 '13 at 03:06
0

Your array is created like this: a[[]]

So you can only access a[0][0] and a[0][1], and a[1] returns undefined.

You can create your array like this. a = [[], []];, this will solve your problem.

Kaeros
  • 1,138
  • 7
  • 7