Start learning free

LESSON 116 OF 289 · ARRAYS - CHALLENGES

Center of the Grid in TypeScript: 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

    TYPESCRIPT · ARRAYS - CHALLENGES

    Center Of The Grid in TypeScript, explained

    Problem overview

    The method centerOfGrid receives a 3×3 grid of integers and returns the number in the middle row and the middle column. A grid is a table of numbers made of rows: this one has 3 rows with 3 numbers each. Rows and columns are counted from 0, so the center is row 1, column 1. For [[1, 2, 3], [4, 5, 6], [7, 8, 9]] the answer is 5. For [[9, 8, 7], [6, -5, 4], [3, 2, 1]] it is -5: the center can be any integer, a negative one too.

    Approach

    In TypeScript a grid is an array whose items are arrays. Its type is number[][]: an array of number[], where each number[] is one row. Reading it takes two steps, and each one uses square brackets:

    • grid[row] is a whole row, for example [4, 5, 6] for row 1 of the first example;
    • grid[row][col] is the number in that row at that column.

    Indexes start at 0, so a row with 3 numbers has the columns 0, 1 and 2, and the middle one is 1. The same holds for the 3 rows. The center is grid[1][1].

    Step by step

    1. Pick the middle row. grid[1] is the array in the middle.
    2. Pick the middle column of that row. Adding a second index, grid[1][1], reads the number you want.
    3. Return it. The whole function is one return statement.

    The solution in TypeScript

    class Solution {
        centerOfGrid(grid: number[][]): number {
            return grid[1][1];
        }
    }
    

    Both indexes are evaluated from left to right. First grid[1] gives a row, and then [1] is read from that row. For the second example, grid[1] is [6, -5, 4] and grid[1][1] is -5.

    Rows are arrays too

    Each row is an ordinary array, so everything you know about arrays applies to it. The name grid[1] refers to the actual middle row, not to a copy. If you store it in a variable, let row = grid[1], and then assign row[1] = 50, the grid itself changes to [[1, 2, 3], [4, 50, 6], [7, 8, 9]]. This lesson only reads, so nothing is modified, but the fact explains why a grid is called an array of arrays and why the double index works: the first index finds the row, and the second index works on that row as on any array.

    Reading other cells

    The same notation reaches any cell. For [[1, 2, 3], [4, 5, 6], [7, 8, 9]]:

    ExpressionValue
    grid[0][2]3
    grid[1][1]5
    grid[2][0]7
    grid.length3
    grid[0].length3

    The first index is always the row and the second the column. Writing them in the opposite order mixes the two up, which gives a different cell for any grid that isn't symmetric.

    Edge cases and common mistakes

    • Counting from 1. grid[2][2] is the bottom-right corner, not the center. Indexes start at 0.
    • Using one index. grid[1] is the entire middle row, an array, not a number.
    • Going outside the grid. With only 3 rows, grid[3] is undefined, and reading grid[3][0] stops the program with TypeError: Cannot read properties of undefined (reading '0'). The lesson's grids always have 3 rows of 3 numbers, so index 1 always exists.
    • Other grid sizes. This solution is written for 3×3. For a grid whose side is odd, the middle index would be the side divided by 2, rounded down.
    • Negative values. A center such as -5 needs no special handling. The function returns whatever is stored there.

    Testing

    • [[1, 2, 3], [4, 5, 6], [7, 8, 9]] gives 5.
    • [[0, 0, 0], [0, 7, 0], [0, 0, 0]] gives 7.
    • [[9, 8, 7], [6, -5, 4], [3, 2, 1]] gives -5.
    Keep going with TypeScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free