diff --git a/Maths/RowEchelon.js b/Maths/RowEchelon.js index c773bb80a9..1e32b05db0 100644 --- a/Maths/RowEchelon.js +++ b/Maths/RowEchelon.js @@ -114,7 +114,7 @@ const rowEchelon = (matrix) => { let numRows = matrix.length let numCols = matrix[0].length - let result = matrix + let result = matrix.map((row) => [...row]) // Iterate through the rows (i) and columns (j) of the matrix. for (let i = 0, j = 0; i < numRows && j < numCols; ) { diff --git a/Maths/test/RowEchelon.test.js b/Maths/test/RowEchelon.test.js index 5575bc6d39..12059cef23 100644 --- a/Maths/test/RowEchelon.test.js +++ b/Maths/test/RowEchelon.test.js @@ -86,4 +86,16 @@ describe('Determinant', () => { ])('Should return the error message.', (matrix, expected) => { expect(() => rowEchelon(matrix)).toThrowError(expected) }) + + test('Should not mutate the input matrix.', () => { + const matrix = [ + [1, 2, 3], + [4, 5, 6], + [7, 8, 10] + ] + const original = matrix.map((row) => [...row]) + const result = rowEchelon(matrix) + expect(matrix).toEqual(original) + expect(result).not.toBe(matrix) + }) })