← Back to challenges

Rotate a 2D Array 90 Degrees Clockwise

JavaScriptHardarrays

Instructions

Create a function that returns an 2D array where the elements have been rotated 90 degrees. The provided array will contain three elements, each being an array with three elements as unique integers.

The desired output would return a new 2D array where the contents of the original array have been rotated 90 degrees clockwise. So row 1 from the original array becomes column 3 for the returned array, row 2 becomes column 2 and row 3 becomes column 1.

Examples

rotate([
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
]) ➞ [
  [7, 4, 1],
  [8, 5, 2],
  [9, 6, 3]
]

rotate([
  ["a", "b", "c"],
  ["d", "e", "f"],
  ["g", "h", "i"]
]) ➞ [
  ["g", "d", "a"],
  ["h", "e", "b"],
  ["i", "f", "c"]
]

Notes

N/A

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.