JSFiddle - React, Tailwind, and code Playground

by kkdaily

JavaScript

/*
	[
  	[1, 2],
  	[3, 4]
  ]

	[
  	[3, 1],
    [4, 2]
  ]


*/

const rotate = (matrix) => {
	// first element of first row -> last element of first row
  // last element of first row -> last element of last row
  
  // ith element of ith row -> row.length - i of the ith row
  const result = matrix.map((row, rowIndex) => {
  	// [1, 2], 0
    return row.map((num, numIndex) => {
    	// 1, 0
      return matrix[row.length - j - 1][i];
    })
  })
  
  return result
}

const a = rotate([[1, 2], [3, 4]])
alert(a)