Skip to content

Commit 7f6a7b6

Browse files
committed
fix: add error throw and general code improvements
1 parent a94b419 commit 7f6a7b6

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

Maths/Determinant.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
2-
* Given a square matrix, find its determinant.
2+
* Given a square matrix, find its determinant using Laplace Expansion.
3+
* Time Complexity : O(n!)
34
*
45
* For more info: https://en.wikipedia.org/wiki/Determinant
56
*
@@ -27,15 +28,13 @@ const subMatrix = (matrix, i, j) => {
2728
for (let x = 0; x < matrixSize; x++) {
2829
if (x === i) {
2930
continue
30-
} else {
31-
subMatrix.push([])
32-
for (let y = 0; y < matrixSize; y++) {
33-
if (y === j) {
34-
continue
35-
} else {
36-
subMatrix[subMatrix.length - 1].push(matrix[x][y])
37-
}
31+
}
32+
subMatrix.push([])
33+
for (let y = 0; y < matrixSize; y++) {
34+
if (y === j) {
35+
continue
3836
}
37+
subMatrix[subMatrix.length - 1].push(matrix[x][y])
3938
}
4039
}
4140
return subMatrix
@@ -57,10 +56,10 @@ const determinant = (matrix) => {
5756
matrix.length === 0 ||
5857
!Array.isArray(matrix[0])
5958
) {
60-
return 'Input is not a valid 2D matrix.'
59+
throw new Error('Input is not a valid 2D matrix.')
6160
}
62-
if (isMatrixSquare(matrix) === false) {
63-
return 'Square matrix is required.'
61+
if (!isMatrixSquare(matrix)) {
62+
throw new Error('Square matrix is required.')
6463
}
6564
let numCols = matrix[0].length
6665
if (numCols === 1) {

0 commit comments

Comments
 (0)