diff --git a/src/algorithms/uncategorized/square-matrix-rotation/README.md b/src/algorithms/uncategorized/square-matrix-rotation/README.md index f58130c2..34ef47f1 100644 --- a/src/algorithms/uncategorized/square-matrix-rotation/README.md +++ b/src/algorithms/uncategorized/square-matrix-rotation/README.md @@ -92,13 +92,13 @@ A B C / / • / • • -And not let's do horizontal reflection: +And now let's do horizontal reflection: A → → B → → C → → -The string has been rotated to 90 degree. +The string has been rotated to 90 degree: • • A • • B diff --git a/src/algorithms/uncategorized/square-matrix-rotation/squareMatrixRotation.js b/src/algorithms/uncategorized/square-matrix-rotation/squareMatrixRotation.js index 139c73ec..f2a52a7a 100644 --- a/src/algorithms/uncategorized/square-matrix-rotation/squareMatrixRotation.js +++ b/src/algorithms/uncategorized/square-matrix-rotation/squareMatrixRotation.js @@ -8,19 +8,28 @@ export default function squareMatrixRotation(originalMatrix) { // Do top-right/bottom-left diagonal reflection of the matrix. for (let rowIndex = 0; rowIndex < matrix.length; rowIndex += 1) { for (let columnIndex = rowIndex + 1; columnIndex < matrix.length; columnIndex += 1) { - const tmp = matrix[columnIndex][rowIndex]; - matrix[columnIndex][rowIndex] = matrix[rowIndex][columnIndex]; - matrix[rowIndex][columnIndex] = tmp; + // Swap elements. + [ + matrix[columnIndex][rowIndex], + matrix[rowIndex][columnIndex], + ] = [ + matrix[rowIndex][columnIndex], + matrix[columnIndex][rowIndex], + ]; } } // Do horizontal reflection of the matrix. for (let rowIndex = 0; rowIndex < matrix.length; rowIndex += 1) { for (let columnIndex = 0; columnIndex < matrix.length / 2; columnIndex += 1) { - const mirrorColumnIndex = matrix.length - columnIndex - 1; - const tmp = matrix[rowIndex][mirrorColumnIndex]; - matrix[rowIndex][mirrorColumnIndex] = matrix[rowIndex][columnIndex]; - matrix[rowIndex][columnIndex] = tmp; + // Swap elements. + [ + matrix[rowIndex][matrix.length - columnIndex - 1], + matrix[rowIndex][columnIndex], + ] = [ + matrix[rowIndex][columnIndex], + matrix[rowIndex][matrix.length - columnIndex - 1], + ]; } }