JavaScript Arrays

I am taking a week off from work to do some training. This will be an independent study. Time to put my JavaScript studies to work on a real project. I needed to represent the floor space of a dungeon. Easy enough. I would just roll out a two dimensional array. I am sure I had done this before. So I kicked out some JavaScript code:

var my_2D_Array = new Array(Array());

Slick right? I got an array of arrays. In other words, I should have a two dimensional array. Wrong. When I tried to run my code, it bombed when trying to access elements in this array. What was up? It took a while to figure it out. I should have just made sure I fully understand two dimensional array.

The outer array will be the container for the rows in my array. I need 25 rows total. Therefore I need to add two items to that outer list. In my code, I was only added one item to the list. Thus I was only getting one row. I needed to add 25 different arrays to my outer array. I did this with a loop. Then I was good to go.

Since the out array contains the rows, I need to use the vertival y value to index into the outer arrray. Some my code will look like this:

my_2D_Array[y][x] = 'x';

Yeah. That looks a bit weird. Don't you usually access 2D arrays like arr[x][y]? Well not in JavaScript. I also had a little trouble getting my fonts to appear monospaced. But that is a story for a future blog post.