From 9f4fbb830f0dde57e266a3f0504d235c6d73e666 Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni <213192267+Aditya2375@users.noreply.github.com> Date: Tue, 24 Nov 2026 12:21:31 +0530 Subject: [PATCH 1/2] fix: stop rowEchelon mutating its input matrix Change result assignment to create a copy of the matrix. --- Maths/RowEchelon.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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; ) { From 29d8b6e21a54f174b40d97432592fef4017df783 Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni <213192267+Aditya2375@users.noreply.github.com> Date: Tue, 24 Nov 2026 12:23:05 +0530 Subject: [PATCH 2/2] test: cover rowEchelon leaving the input matrix unchanged --- Maths/test/RowEchelon.test.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) 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) + }) })