Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,987 questions

51,931 answers

573 users

How to rotate square matrix 90 degrees to the left in JavaScript

2 Answers

0 votes
function print_matrix(matrix) { 
    matrix.forEach(row => console.log(row.join(" ")));
} 
  
function rotate_matrix_90_degrees_left(matrix) { 
  const len = matrix.length;
  
  for (let i = 0; i < len / 2; i++) { 
        for (let j = i; j < len - i - 1; j++) { 
            let tmp = matrix[i][j]; 
            
            matrix[i][j] = matrix[j][len - 1 - i]; 
            matrix[j][len - 1 - i] = matrix[len - 1 - i][len - 1 - j]; 
            matrix[len - 1 - i][len - 1 - j] = matrix[len - 1 - j][i]; 
            matrix[len - 1 - j][i] = tmp; 
        } 
    } 
} 

const matrix = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
];
    
rotate_matrix_90_degrees_left(matrix); 
  
print_matrix(matrix); 
 
 
  
/*
run:
  
3 6 9
2 5 8
1 4 7
         
*/

 



answered May 18, 2019 by avibootz
edited Oct 6, 2025 by avibootz
0 votes
function print_matrix(matrix) { 
    matrix.forEach(row => console.log(row.join(" ")));
} 

/**
 * Rotates a square matrix 90 degrees to the left (counterclockwise).
 * @param {number[][]} matrix - The square matrix to rotate.
 */
function rotateMatrix90DegreesLeft(matrix) {
    const len = matrix.length;

    // Validate input: Ensure it's a square matrix
    if (!Array.isArray(matrix) || !matrix.every(row => Array.isArray(row) && row.length === len)) {
        throw new Error("Input must be a square matrix.");
    }

    // Perform the rotation in-place
    for (let layer = 0; layer < Math.floor(len / 2); layer++) {
        const first = layer;
        const last = len - 1 - layer;

        for (let i = first; i < last; i++) {
            const offset = i - first;

            // Save the top element
            const top = matrix[first][i];

            // Move right to top
            matrix[first][i] = matrix[i][last];

            // Move bottom to right
            matrix[i][last] = matrix[last][last - offset];

            // Move left to bottom
            matrix[last][last - offset] = matrix[last - offset][first];

            // Move top to left
            matrix[last - offset][first] = top;
        }
    }
}

const matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
];

rotateMatrix90DegreesLeft(matrix);

console.log("Matrix After 90° Left Rotation:");
print_matrix(matrix);


/*
run:

Matrix After 90° Left Rotation:
3 6 9
2 5 8
1 4 7

*/

 



answered Oct 6, 2025 by avibootz
...