JSFiddle - React, Tailwind, and code Playground

by Krishna Ananthi

HTML

1 2 3 
4 5 6
7 8 9

7 4 1
8 5 2
9 6 3

JavaScript

function rotate(matrix) {
				console.log(matrix);
        // Reverse the matrix vertically
        matrix.reverse();
				console.log(matrix);
        // Transpose the matrix
        for (let i = 0; i < matrix.length; i++) {
            for (let j = i; j < matrix[i].length; j++) {
                [matrix[i][j], matrix[j][i]] = [matrix[j][i], matrix[i][j]];
            }
        }
        return matrix;
    }
    
    console.log(rotate([[1,2,3],
  [4,5,6],
  [7,8,9]]))