{"record":{"id":"c406bae0a6cfb507","repo":"TheAlgorithms/JavaScript","slug":"grid-must-only-contain-0s-and-1s","errorCode":null,"errorMessage":"Grid must only contain 0s and 1s","messagePattern":"Grid must only contain 0s and 1s","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Backtracking/RatInAMaze.js","lineNumber":37,"sourceCode":" * - 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.\n * @param y The current Y coordinate.\n * @param solution The current solution matrix.\n * @param path The path we took to get from the source cell to the current location.\n * @returns {string|boolean} Either the path to the target cell or false.\n */\nfunction getPathPart(grid, x, y, solution, path) {","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Backtracking/RatInAMaze.js#L19-L55","documentation":"Thrown by validateGrid() when any cell is not strictly 0 or 1. The maze uses 1 for open path and 0 for wall, and isSafe() compares grid[y][x] === 1 with strict equality, so the string \"1\", booleans, null, or 2/3 would break path detection.","triggerScenarios":"Cells containing \"1\" (string from JSON/text), 2, true/false, null, undefined, NaN, or any numeric value other than 0 or 1.","commonSituations":"Reading the maze from a text file or CSV where values arrive as strings, or storing tristate flags (e.g. 2 = visited) inside the source grid.","solutions":["Normalize all cells to numbers 0 or 1 before solving.","If parsing from text, map with Number() or +cell and reject anything outside {0,1}.","Keep a separate visited/seen structure instead of encoding extra states into the grid."],"exampleFix":"// before\nconst grid = rawText.split('\\n').map(r => r.split(''))\n// after\nconst grid = rawText\n  .trim().split('\\n')\n  .map(r => r.trim().split('').map(Number))","handlingStrategy":"validation","validationCode":"function isBinaryGrid(g) {\n  return Array.isArray(g) && g.length > 0 &&\n    g.every(r => Array.isArray(r) && r.every(c => c === 0 || c === 1));\n}\n// grid = grid.map(r => r.map(Number)); before solving","typeGuard":"/** @param {unknown} g @returns {g is (0|1)[][]} */\nfunction isBinaryGrid(g) {\n  return Array.isArray(g) && g.length > 0 &&\n    g.every(r => Array.isArray(r) && r.every(c => c === 0 || c === 1));\n}","tryCatchPattern":"try { solve(grid); }\ncatch (e) {\n  if (e instanceof TypeError && /0s and 1s/.test(e.message)) {\n    grid = grid.map(r => r.map(c => Number(c)));\n  } else throw e;\n}","preventionTips":["Always coerce maze text to numbers at parse time.","Treat the grid as read-only 0/1; store extra state elsewhere.","Reject values outside {0,1} when ingesting the maze."],"tags":["backtracking","maze","grid","type-coercion"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}