Practice

Problem 6 of 12

Problem · Medium · 20 min

Turn the picture

  • Medium
  • Grids
  • Arrays

A picture is a grid of values. The grid is an array of rows, and every row has the same length.

Write rotateRight(grid). It returns a new grid that shows the same picture after a quarter turn clockwise (90 degrees to the right).

For example, this grid:

1 2 3
4 5 6

becomes this grid after the turn:

4 1
5 2
6 3

In code, [[1, 2, 3], [4, 5, 6]] becomes [[4, 1], [5, 2], [6, 3]].

  • The grid does not have to be a square.
  • An empty grid gives an empty grid.

Examples

Call Result
rotateRight([[1, 2, 3], [4, 5, 6]]) [[4, 1], [5, 2], [6, 3]]
rotateRight([[1, 2], [3, 4]]) [[3, 1], [4, 2]]
rotateRight([]) []

Tasks

2 to do

  1. Task 1, not checked yet. Make rotateRight pass the example tests.

  2. Task 2, not checked yet. Make rotateRight pass the hidden tests.

Task 1 of 2

Make rotateRight pass the example tests.

All tasks

main.js

Ctrl+Enter runs your code. Ctrl+Shift+Enter runs all the tests. Press Esc, then Tab, to leave the editor.

Output

Press Run to see your result

Your output shows here. Press Run to try your code.