So, I was having this problem with arrays in JavaScript.
I initialized some variables as shown here:
var world = new Array;
var monsters = new Array;
var items = new Array;
var x = 0;
var y = 0;
and tried to run this code on it:
while (y <= 49) { //generate world
world[x][y] = parseInt(Math.floor(Math.random()*2)); //0 = flatland, 1 = desert, 2 = mountain, 3 = swamp
x++;
if (x >= 49) {
x = 0;
y++;
}
}
x = 0;
y = 0;
but I get presented with this error:
“TypeError: can’t convert undefined to object”
on the Math.random() line.
I’ve tried everything I can think of, but it still throws that error.
Any help will be greatly appreciated!
world[x][y]is not initialized. That’s the problem. You need to intialize below thewhilestatement