Let’s rotate a matrix clockwise! JavaScript Beginners

Tina Luk
Tina Luk
Aug 16, 2020 · 4 min read
Photo by Honey Yanibel Minaya Cruz on Unsplash

Given an n x n matrix, how can we rotate it by 90 degrees clockwise? This is a standard algorithm problem, let’s look at a straightforward solution with visuals before we refactor and optimize our code.

Our goal is to write a function that accepts an input matrix of any n x n size and returns an output of a rotated matrix. See the example of a 3 x 3 matrix below, an array of arrays representation for our input and output.

This blog will cover the following and use a 3 x 3 matrix example:

  1. Visuals for each step of the matrix rotation process
  2. How to flip matrix on major and minor diagonal
  3. Other solutions using JavaScript’s built-in methods

Let’s rotate this matrix…

3 x 3 matrix rotate by 90 degrees

Comparing the input matrix and the output, the columns are now rows, but reversed. For example, the first column in the input has now become the first row in the output but reversed.

What are the steps to rotate the matrix?

Steps to rotate matrix

To access each value in the matrix, a simple nested loop can be used.

  1. The matrix is flipped along the major diagonal (top left to bottom right). This will transpose the columns to be rows. This is done by swapping the values on the upper half of the diagonal with the lower half. For example, the value at [row: index 0][column: index 1] is 2 to be replaced by the value at [row: index 1][column: index 0] which is 4 and vice versa. Remember to ensure the swap only takes place one time, the inner nested loop (column index) must be initialized correctly.
  2. Reverse each row in the matrix. This can be executed with JavaScript’s built-in reverse method or with a for loop swapping values at each end until the center is reached.

Now we know how to flip along the major diagonal and rotate the matrix. Let’s take it a step further.

How to flip matrix along the minor diagonal?

The minor diagonal the from the top right to the bottom left.

Steps to flip along the minor diagonal

This problem without the rotated matrix, may be cumbersome to solve with swapping index values. Swapping the index value along the minor diagonal is not as simple as the major diagonal.

However, with the rotated matrix in play, we can visually see the solution is simple. Just reverse (upside down) the rotated matrix! The steps are:

  1. Rotate matrix by 90 degrees
  2. Reverse order of entire matrix, flip upside down. Bottom row is now on top and so on.

Let’s look at the code. The function expressions have been isolated by different ways to manipulate the matrix.

Summary of the different functions expressions:

  • Rotate matrix: flip along major diagonal then reverse each row
  • Flip major diagonal: will swap each value on upper half of diagonal with lower half. To ensure the swap does not occur twice, initialize the inner loop (column index) to start at the outer loop (row index).
  • Flip minor diagonal: rotate matrix then reverse entire matrix

Other Solution to rotate with JavaScript’s built-in Map method

Leveraging JavaScript’s map method, we can compress our code to rotate the matrix even further as shown below.

For a deeper dive, on this solution the reference below is excellent!

Rotate matrix with map

To break down this solution high level:

  1. Reverse entire matrix
  2. Use outer map to get all the column index’s
  3. Use inner map to change each row with column values

Hope this was helpful for someone!

I am ending week 6 of Hack Reactor. My 10 month old is wildly crawling and climbing everywhere. Our exercise the last 2 weeks included solving N-Queens and learning about HTTP, REST API, mechanisms of how internet traffic is transmitted and MVC. With jQuery, we got some practice getting data from a serve without a page refresh with Parse API as the back-end. Definitely, feels like there constantly is a lot to learn!

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Sign up for Best Stories

By Dev Genius

The best stories sent monthly to your email. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Tina Luk

Written by

Tina Luk

Mommy and student figuring things out

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Tina Luk

Written by

Tina Luk

Mommy and student figuring things out

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store