{"record":{"id":"f4918540ed837ab1","repo":"TheAlgorithms/JavaScript","slug":"input-is-not-a-valid-2d-matrix","errorCode":null,"errorMessage":"Input is not a valid 2D matrix.","messagePattern":"Input is not a valid 2D matrix\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Maths/Determinant.js","lineNumber":59,"sourceCode":"}\n\nconst isMatrixSquare = (matrix) => {\n  let numRows = matrix.length\n  for (let i = 0; i < numRows; i++) {\n    if (numRows !== matrix[i].length) {\n      return false\n    }\n  }\n  return true\n}\n\nconst determinant = (matrix) => {\n  if (\n    !Array.isArray(matrix) ||\n    matrix.length === 0 ||\n    !Array.isArray(matrix[0])\n  ) {\n    throw new Error('Input is not a valid 2D matrix.')\n  }\n  if (!isMatrixSquare(matrix)) {\n    throw new Error('Square matrix is required.')\n  }\n  let numCols = matrix[0].length\n  if (numCols === 1) {\n    return matrix[0][0]\n  }\n  let result = 0\n  let setIndex = 0\n  for (let i = 0; i < numCols; i++) {\n    result +=\n      Math.pow(-1, i) *\n      matrix[setIndex][i] *\n      determinant(subMatrix(matrix, setIndex, i))\n  }\n  return result\n}","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Maths/Determinant.js#L41-L77","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nconst d = determinant(flatArray) // flatArray is [1,2,3,4]\n\n// after\nfunction toMatrix(flat, cols) {\n  return Array.from({ length: flat.length / cols }, (_, i) =>\n    flat.slice(i * cols, i * cols + cols))\n}\nconst d = determinant(toMatrix(flatArray, 2))","handlingStrategy":"type-guard","validationCode":"if (!Array.isArray(matrix) || matrix.length === 0 || !Array.isArray(matrix[0])) {\n  throw new Error('input must be a non-empty 2D array')\n}\nconst d = determinant(matrix)","typeGuard":"const is2DArray = (v) => Array.isArray(v) && v.length > 0 && v.every(row => Array.isArray(row))","tryCatchPattern":"try {\n  d = determinant(matrix)\n} catch (e) {\n  if (e instanceof Error && /not a valid 2D matrix/.test(e.message)) {\n    // not 2D — convert or reject\n  } else throw e\n}","preventionTips":["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."],"tags":["validation","matrix","array","linear-algebra","input-validation"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}