{"record":{"id":"ffd62672ae3228a9","repo":"TheAlgorithms/JavaScript","slug":"square-matrix-is-required","errorCode":null,"errorMessage":"Square matrix is required.","messagePattern":"Square matrix is required\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Maths/Determinant.js","lineNumber":62,"sourceCode":"  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}\nexport { determinant }\n","sourceCodeStart":44,"sourceCodeEnd":79,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Maths/Determinant.js#L44-L79","documentation":"Thrown by determinant(matrix) (Determinant.js:62) as a plain Error when the matrix passes the structural check but is not square. The helper isMatrixSquare (Determinant.js:43) verifies that every row has length equal to the number of rows. The Laplace expansion algorithm requires a square matrix, so a non-square input is rejected. A 1x1 matrix returns its single element without throwing.","triggerScenarios":"Call determinant([[1,2,3],[4,5,6]]) — a 2x3 matrix; determinant([[1,2],[3,4],[5,6]]) — a 3x2 matrix; a matrix with a ragged row like [[1,2],[3]] returns false from isMatrixSquare and throws here.","commonSituations":"Loading data with inconsistent row lengths from CSV; transposing or slicing that changed dimensions; passing a data table with a header row of different length; construction loop with an off-by-one column count.","solutions":["Verify squareness before calling: m.every(row => row.length === m.length).","If you need the determinant of a non-square matrix, reconsider — it is undefined; you may want SVD or a different decomposition.","Validate row lengths during data loading and reject ragged input early.","Log matrix dimensions at the call site to catch dimension mismatches during debugging."],"exampleFix":"// before\nconst d = determinant(m) // throws if m is not square\n\n// after\nconst isSquare = Array.isArray(m) && m.length > 0 &&\n  m.every(row => Array.isArray(row) && row.length === m.length)\nif (!isSquare) throw new Error('expected a square matrix')\nconst d = determinant(m)","handlingStrategy":"validation","validationCode":"const isSquare = Array.isArray(matrix) && matrix.length > 0 &&\n  matrix.every(row => Array.isArray(row) && row.length === matrix.length)\nif (!isSquare) throw new Error('expected a square matrix')\nconst d = determinant(matrix)","typeGuard":"const isSquareMatrix = (v) => Array.isArray(v) && v.length > 0 &&\n  v.every(row => Array.isArray(row) && row.length === v.length)","tryCatchPattern":"try {\n  d = determinant(matrix)\n} catch (e) {\n  if (e instanceof Error && /Square matrix/.test(e.message)) {\n    // non-square — reconsider whether determinant is the right operation\n  } else throw e\n}","preventionTips":["Validate row.length === matrix.length for every row before calling.","Reject ragged data during loading rather than at the math call site.","Determinants are undefined for non-square matrices — do not try to force one."],"tags":["validation","matrix","square-matrix","linear-algebra"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}