0

I need to know how to work with a 2D Array and nested loop.

This is the code that I have so far -

    var x = parseInt(window.prompt("inter the row:"));
    var y = parseInt(window.prompt("inter the column:"));
    var [,]names = new Array[x,y];

    for (var i=0; i<x;i++)
    {
         for(var j=0; j<y;j++)
         {
           names [i,j]=parseInt(window.prompt(" "));
         }
    }
            for (var i=0; i<x;i++)
    {
         for(var j=0; j<y;j++)
         {
           document.write(names[i,j]);
         }
         document.write("<br>");
    }

This code does not work, and I need help figuring out what might be wrong with it.

whereisSQL
  • 638
  • 2
  • 13
  • 23

1 Answers1

0

You can do this:

var names = [];
for(var i = 0; i < x; i++){
  names[i] = [];
  for(var j = 0; j < y; j++){
    names[i][j] = parseInt(...);
  }
}
TheDude
  • 3,796
  • 2
  • 28
  • 51