Start learning free

LESSON 113 OF 285 · ARRAYS - CHALLENGES

Center of the Grid in JavaScript: Nested Arrays

Practice Read the full explanation

Given a 3×3 grid of integers grid, return the number in its center: the one in the middle row and the middle column.

A grid is a table of numbers made of rows. This one has 3 rows, and every row has 3 numbers, one in each column. Rows and columns are counted from 0, so the middle row is row 1 and the middle column is column 1.

 

Example 1:

Input: grid = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Output: 5
Explanation: The middle row is [4, 5, 6], and the number in its middle column is 5.

Example 2:

Input: grid = [[0, 0, 0], [0, 7, 0], [0, 0, 0]]
Output: 7
Explanation: 7 is in row 1, column 1: the center of the grid.

Example 3:

Input: grid = [[9, 8, 7], [6, -5, 4], [3, 2, 1]]
Output: -5
Explanation: The center can be any number, a negative one too.

 

Constraints:

  • grid has exactly 3 rows, and every row has exactly 3 numbers.
  • Every number is an integer between -1000 and 1000.
STEP 1 OF 1
    Ctrl + Enter to check

    JAVASCRIPT · ARRAYS - CHALLENGES

    Center Of The Grid in JavaScript, explained

    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

    1. Pick the middle row with grid[1].
    2. 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.

    Keep going with JavaScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free