JavaScript: Matrix rotation

Last update: 22 Aug 23:20
0
Students

matrix.js

Write and export rotateLeft() and rotateRight() functions that rotate the matrix to the left (counterclockwise) and to the right (clockwise) respectively.

  • Matrix is implemented via array
  • Functions should return a new matrix without changing the initial one

Examples:

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

rotateLeft(matrix);
// [
//   [4, 8, 2],
//   [3, 7, 1],
//   [2, 6, 0],
//   [1, 5, 9],
// ]

rotateRight(matrix);
// [
//   [9, 5, 1],
//   [0, 6, 2],
//   [1, 7, 3],
//   [2, 8, 4],
// ]

For full access to the challenge you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests