I'm trying to create a 2D array with javascript that I can ultimately put inside a nested loop to extract X/Y information.
What am I doing wrong here?
function createMatrix(){
let colcount = 0;
let rowcount = 0;
var pixel_array = new Array();
var y_array = new Array();
for (var i = 0; i <= 100; i++){
var checker = (i+1) % 10;
if(checker == 0){
y_array.push(i);
//create new X array
pixel_array[rowcount] = [];
//push column data into row array
pixel_array[rowcount].push(y_array);
//create new Y array
var y_array = new Array();
//increment row counter
//reset column counter
parseInt(rowcount++);
colcount = 0;
}else{
y_array.push(i);
parseInt(colcount++);
}
}
//sanity check: spit out the matrix
for (var x=0; x<10;x++){
for(var y=0; y<10; y++){
console.log(pixel_array[x][y]);
}
}
}
I was expecting to call a specific X/Y 'coordinate' and extract the information from that 'cell'. However, I'm getting an error that basically says the [Y] part of the array is not defined.
Looking at console.log and console.table - I can see the X array is filled, but it isn't like I'd expect, just a list of numbers not another array.
Edit: To be more specific, my goal is to create a 2D array from a single For loop. The nested for loop at the bottom of the code is shown as an example of how I would like to call the 'cells' [X][Y].