{"record":{"id":"fe3daaeb5dc0a8fb","repo":"TheAlgorithms/JavaScript","slug":"grid-must-be-a-square","errorCode":null,"errorMessage":"Grid must be a square","messagePattern":"Grid must be a square","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Backtracking/RatInAMaze.js","lineNumber":31,"sourceCode":"/**\n * Checks if the given grid is valid.\n *\n * A grid needs to satisfy these conditions:\n * - must not be empty\n * - must be a square\n * - must not contain values other than {@code 0} and {@code 1}\n *\n * @param grid The grid to check.\n * @throws TypeError When the given grid is invalid.\n */\nfunction validateGrid(grid) {\n  if (!Array.isArray(grid) || grid.length === 0)\n    throw new TypeError('Grid must be a non-empty array')\n\n  const allRowsHaveCorrectLength = grid.every(\n    (row) => row.length === grid.length\n  )\n  if (!allRowsHaveCorrectLength) throw new TypeError('Grid must be a square')\n\n  const allCellsHaveValidValues = grid.every((row) => {\n    return row.every((cell) => cell === 0 || cell === 1)\n  })\n  if (!allCellsHaveValidValues)\n    throw new TypeError('Grid must only contain 0s and 1s')\n}\n\nfunction isSafe(grid, x, y) {\n  const n = grid.length\n  return x >= 0 && x < n && y >= 0 && y < n && grid[y][x] === 1\n}\n\n/**\n * Attempts to calculate the remaining path to the target.\n *\n * @param grid The full grid.\n * @param x The current X coordinate.","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Backtracking/RatInAMaze.js#L13-L49","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pad/trim every row so row.length === grid.length.","Build the grid programmatically with a fixed N and assert dimensions before solving.","Validate with grid.every(r => r.length === grid.length) at the call site."],"exampleFix":"// before\nsolveRatInAMaze([[1,0,1],[0,1]])\n// after\nconst grid = [[1,0,1],[0,1,0],[0,0,1]] // square 3x3\nsolveRatInAMaze(grid)","handlingStrategy":"type-guard","validationCode":"function isSquareMatrix(g) {\n  return Array.isArray(g) && g.length > 0 &&\n    g.every(r => Array.isArray(r) && r.length === g.length);\n}","typeGuard":"/** @param {unknown} g @returns {g is unknown[][]} */\nfunction isSquare(g) {\n  return Array.isArray(g) && g.length > 0 &&\n    g.every(r => Array.isArray(r) && r.length === g.length);\n}","tryCatchPattern":"try { solve(grid); }\ncatch (e) {\n  if (e instanceof TypeError && e.message === 'Grid must be a square') {\n    // log dimensions and reject input\n  } else throw e;\n}","preventionTips":["Construct grids with a single N constant for rows and columns.","Add a dimension assert right after parsing: console.assert(grid.every(r => r.length === grid.length)).","Use a matrix builder helper that enforces squareness."],"tags":["backtracking","maze","grid","dimension-validation"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}