TheAlgorithms/JavaScript · error · TypeError

Grid must be a square

Error message

Grid must be a square

What it means

Thrown by validateGrid() when every row's length does not equal the grid's row count. The algorithm assumes a square N x N maze (it reads n = grid.length and bounds-checks x,y against n), so a jagged or rectangular grid would index out of bounds or misread walls.

Source

Thrown at Backtracking/RatInAMaze.js:31

/**
 * Checks if the given grid is valid.
 *
 * A grid needs to satisfy these conditions:
 * - must not be empty
 * - must be a square
 * - must not contain values other than {@code 0} and {@code 1}
 *
 * @param grid The grid to check.
 * @throws TypeError When the given grid is invalid.
 */
function validateGrid(grid) {
  if (!Array.isArray(grid) || grid.length === 0)
    throw new TypeError('Grid must be a non-empty array')

  const allRowsHaveCorrectLength = grid.every(
    (row) => row.length === grid.length
  )
  if (!allRowsHaveCorrectLength) throw new TypeError('Grid must be a square')

  const allCellsHaveValidValues = grid.every((row) => {
    return row.every((cell) => cell === 0 || cell === 1)
  })
  if (!allCellsHaveValidValues)
    throw new TypeError('Grid must only contain 0s and 1s')
}

function isSafe(grid, x, y) {
  const n = grid.length
  return x >= 0 && x < n && y >= 0 && y < n && grid[y][x] === 1
}

/**
 * Attempts to calculate the remaining path to the target.
 *
 * @param grid The full grid.
 * @param x The current X coordinate.

View on GitHub (pinned to 5c39e87a9a)

Solutions

  1. Pad/trim every row so row.length === grid.length.
  2. Build the grid programmatically with a fixed N and assert dimensions before solving.
  3. Validate with grid.every(r => r.length === grid.length) at the call site.

Example fix

// before
solveRatInAMaze([[1,0,1],[0,1]])
// after
const grid = [[1,0,1],[0,1,0],[0,0,1]] // square 3x3
solveRatInAMaze(grid)
Defensive patterns

Strategy: type-guard

Validate before calling

function isSquareMatrix(g) {
  return Array.isArray(g) && g.length > 0 &&
    g.every(r => Array.isArray(r) && r.length === g.length);
}

Type guard

/** @param {unknown} g @returns {g is unknown[][]} */
function isSquare(g) {
  return Array.isArray(g) && g.length > 0 &&
    g.every(r => Array.isArray(r) && r.length === g.length);
}

Try / catch

try { solve(grid); }
catch (e) {
  if (e instanceof TypeError && e.message === 'Grid must be a square') {
    // log dimensions and reject input
  } else throw e;
}

Prevention

When it happens

Trigger: Passing a jagged array like [[1,0,1],[0,1]], a rectangular grid like a 2x3 [[1,0,1],[0,1,0]], or any matrix where at least one row.length !== grid.length.

Common situations: Hand-editing a maze and forgetting a cell, transposing data from a row-major source with mismatched dimensions, or copy-pasting rows of different widths.

Related errors


AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13). Data as JSON: /api/errors/fe3daaeb5dc0a8fb. Report an issue: GitHub.