2d arrays inside 2d arrays inside 2d arrays... ugh

2d arrays inside 2d arrays inside 2d arrays... ugh
0

Hi guys and girls. I’m stuck on something and I’m hoping one of you can at least point me in the right direction. I’ve created side-by-side 2d arrays to form 2 grids, but I need to print letters to the left of each grid to establish coordinates. I’ve been working on this all day and I can’t seem to figure out how to do it without printing across the entire array. Can someone show me (or give me a solid idea) where I should be plugging in the char increment for the row marker? I’m stumped. :exploding_head:

	for (int i = 0; i < grid1.length; i++)
		{	
			for (int j = 0; j < grid1.length; j++)
			{				
				grid1[i][j] = ' ';
				System.out.print("|_"+grid1[i][j]+ "_");
			}
			
			System.out.print("    ");
						
			for (int j = 0; j < grid1.length; j++)
			{				
				grid2[i][j] = ' ';
				System.out.print("|_"+grid2[i][j]+"_");				
			}
			System.out.println("\n");
		}

//char increment for row marker
char coord1 = 'A';
coord1++

Maybe an example of the output you are hoping to achieve?

what I’m looking to create is:

A |__|__|__|__|__|      A |__|__|__|__|__|
B |__|__|__|__|__|      B |__|__|__|__|__|
C |__|__|__|__|__|      C |__|__|__|__|__|..... etc
  • Print coord1 immediately before each inner for loops.
  • Let coord1++ be the last statement of the outer for loop
1 Like

Something like this?

const makeGrid = (x, y) => [...Array(y)].map((_, idx) => [String.fromCharCode(65 + idx)].concat(Array(x).fill('')));
console.log(makeGrid(3, 3));

The concept here is in three part.

1/ finding the loop that will print horizontally.
2/ finding the loop that will print vertically.
3/ order in which your coordinates will be printed.

Print out one row first.
Wrap that around with another loop to create multiple rows.
Finally figure when you should put the coordinate words on each row. :slight_smile:

1 Like

Thank you!! That did the trick. It seems so obvious after you pointed it out.