TheAlgorithms/JavaScript · error · Error
Input is not a valid 2D matrix.
Error message
Input is not a valid 2D matrix.
What it means
Thrown by determinant(matrix) (Determinant.js:58) as a plain Error when the input is not structurally a 2D array: it fails Array.isArray on the outer value, or the outer array is empty, or the first row (matrix[0]) is not itself an array. This is the structural validation that runs before the squareness check. Passing a 1D array like [1,2,3] triggers it because matrix[0] is a number, not an array.
Source
Thrown at Maths/Determinant.js:59
}
const isMatrixSquare = (matrix) => {
let numRows = matrix.length
for (let i = 0; i < numRows; i++) {
if (numRows !== matrix[i].length) {
return false
}
}
return true
}
const determinant = (matrix) => {
if (
!Array.isArray(matrix) ||
matrix.length === 0 ||
!Array.isArray(matrix[0])
) {
throw new Error('Input is not a valid 2D matrix.')
}
if (!isMatrixSquare(matrix)) {
throw new Error('Square matrix is required.')
}
let numCols = matrix[0].length
if (numCols === 1) {
return matrix[0][0]
}
let result = 0
let setIndex = 0
for (let i = 0; i < numCols; i++) {
result +=
Math.pow(-1, i) *
matrix[setIndex][i] *
determinant(subMatrix(matrix, setIndex, i))
}
return result
}View on GitHub (pinned to 5c39e87a9a)
Solutions
- Ensure the input is an array of arrays: Array.isArray(m) && Array.isArray(m[0]).
- Convert flat data to 2D before calling, e.g. chunk into rows of equal length.
- If using typed arrays, convert with Array.from before passing.
- Guard against empty input at the boundary and return a sensible default.
Example fix
// before
const d = determinant(flatArray) // flatArray is [1,2,3,4]
// after
function toMatrix(flat, cols) {
return Array.from({ length: flat.length / cols }, (_, i) =>
flat.slice(i * cols, i * cols + cols))
}
const d = determinant(toMatrix(flatArray, 2)) Defensive patterns
Strategy: type-guard
Validate before calling
if (!Array.isArray(matrix) || matrix.length === 0 || !Array.isArray(matrix[0])) {
throw new Error('input must be a non-empty 2D array')
}
const d = determinant(matrix) Type guard
const is2DArray = (v) => Array.isArray(v) && v.length > 0 && v.every(row => Array.isArray(row))
Try / catch
try {
d = determinant(matrix)
} catch (e) {
if (e instanceof Error && /not a valid 2D matrix/.test(e.message)) {
// not 2D — convert or reject
} else throw e
} Prevention
- Confirm the outer value and every row are arrays before calling.
- Typed arrays (Int32Array) are not Array.isArray — convert with Array.from.
- Chunk flat arrays into equal-length rows before passing.
When it happens
Trigger: Call determinant([1, 2, 3]) passing a flat array; determinant([]) passing an empty array; determinant('matrix') or determinant(null) passing a non-array; determinant([[1,2],[3,4,5]]) does NOT throw here (it is structurally 2D) but would fail the squareness check next.
Common situations: Flattening a matrix by mistake; receiving a vector instead of a matrix from an upstream op; empty result from a data loader; passing a typed array (Int32Array) which is not Array.isArray.
Related errors
- Square matrix is required.
- Input is not a valid 2D matrix.
- Invalid Triangle sides.
- Invalid Input
- Index out of range
AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13).
Data as JSON: /api/errors/f4918540ed837ab1.
Report an issue: GitHub.