TheAlgorithms/JavaScript · error · TypeError
Grid must only contain 0s and 1s
Error message
Grid must only contain 0s and 1s
What it means
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.
Source
Thrown at Backtracking/RatInAMaze.js:37
* - 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.
* @param y The current Y coordinate.
* @param solution The current solution matrix.
* @param path The path we took to get from the source cell to the current location.
* @returns {string|boolean} Either the path to the target cell or false.
*/
function getPathPart(grid, x, y, solution, path) {View on GitHub (pinned to 5c39e87a9a)
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.
Example fix
// before
const grid = rawText.split('\n').map(r => r.split(''))
// after
const grid = rawText
.trim().split('\n')
.map(r => r.trim().split('').map(Number)) Defensive patterns
Strategy: validation
Validate before calling
function isBinaryGrid(g) {
return Array.isArray(g) && g.length > 0 &&
g.every(r => Array.isArray(r) && r.every(c => c === 0 || c === 1));
}
// grid = grid.map(r => r.map(Number)); before solving Type guard
/** @param {unknown} g @returns {g is (0|1)[][]} */
function isBinaryGrid(g) {
return Array.isArray(g) && g.length > 0 &&
g.every(r => Array.isArray(r) && r.every(c => c === 0 || c === 1));
} Try / catch
try { solve(grid); }
catch (e) {
if (e instanceof TypeError && /0s and 1s/.test(e.message)) {
grid = grid.map(r => r.map(c => Number(c)));
} else throw e;
} Prevention
- 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.
When it happens
Trigger: Cells containing "1" (string from JSON/text), 2, true/false, null, undefined, NaN, or any numeric value other than 0 or 1.
Common situations: 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.
Related errors
AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13).
Data as JSON: /api/errors/c406bae0a6cfb507.
Report an issue: GitHub.