Problem overview
The method centerOfGrid receives grid, a 3×3 array of integers: 3 rows with 3 numbers each. It returns the number in the middle row and the middle column.
For [[1, 2, 3], [4, 5, 6], [7, 8, 9]] the middle row is [4, 5, 6] and its middle number is 5. For [[0, 0, 0], [0, 7, 0], [0, 0, 0]] the answer is 7, and for [[9, 8, 7], [6, -5, 4], [3, 2, 1]] it is -5: the center can be a negative number.
Approach
An array can hold other arrays. Here each row is itself an array of 3 numbers. So grid[row] is a whole row, and grid[row][col] is the number at that row and column.
Rows and columns are counted from 0, so the rows are 0, 1 and 2, and the middle one is 1. The columns work the same way. The center is grid[1][1].
Step by step
- Pick the middle row with
grid[1]. - Pick the middle column of that row with a second index,
[1], and return the number.
Reading the grid cell by cell
For the first example, the four corners are grid[0][0] (1), grid[0][2] (3), grid[2][0] (7) and grid[2][2] (9). The cell to the left of the center is grid[1][0], which is 4. Only grid[1][1] is in both the middle row and the middle column, and it holds 5.
The solution in JavaScript
class Solution {
centerOfGrid(grid) {
return grid[1][1];
}
}
On the examples it returns:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]] -> 5
[[0, 0, 0], [0, 7, 0], [0, 0, 0]] -> 7
[[9, 8, 7], [6, -5, 4], [3, 2, 1]] -> -5
For the first grid, grid[1] alone is [4, 5, 6], and [1] on that row picks the 5.
Why a grid is an array of arrays
JavaScript has no special two-dimensional type. grid.length is 3, the number of rows, and grid[1].length is 3, the number of columns. The middle of any odd-sized square grid can be found the same way as in the lesson about the middle of an array, with Math.floor(grid.length / 2) for the row and for the column. For this 3×3 grid it gives 1 and 1, and grid[1][1] is 5 for the first example. Because the size is fixed here, the lesson writes the indexes directly.
Thinking about the problem
A grid problem is a question about two positions: a row and a column. Write them down before writing code. Here both are the middle, and with indexes starting at 0 the middle of 0, 1 and 2 is 1. Once the two indexes are known, the answer is one expression. The first index always selects a row, so grid[1] is [4, 5, 6] in the first example, and the second index then selects within that row.
Edge cases and common mistakes
- Counting from 1.
grid[2][2]is the bottom-right corner, not the center. The middle of indexes 0, 1 and 2 is 1. - Swapping row and column. The first index picks the row and the second picks the column. For the center both are 1, so the order doesn't matter here, but it does for other cells.
- A single index.
grid[1]returns the whole middle row, not a number.
Testing
Asking for a row that doesn't exist shows how nested access fails. grid[3] is undefined, and grid[3][1] throws a TypeError: Cannot read properties of undefined (reading '1'). With a 3×3 grid and the indexes 1 and 1, that cannot happen.
