Workshop › Tetris-style Blocks › Step 13 of 32
Rotating a piece clockwise is a rule about its matrix: the first column, read from the bottom up, becomes the first
row; the second column the second row, and so on. rotate builds that new matrix and leaves the old one alone.
```js
// Clockwise: the first column, read from the bottom up, becomes the first row.
function rotate(shape) {
return shape[0].map((_, col) => shape.map((row) => row[col]).reverse())
}
```shape.map((row) => row[col]) takes number col from every row: column col, top to bottom..reverse() turns it bottom to top.shape[0].map((_, col) => ...) does that once for each column and collects the results as the new rows. _ is the
name for the element we do not need; only the index col matters.map always makes new arrays, so the original is not changed. This only works for square matrices, which is why
every piece is square.Write the comment and rotate above function fits(.
Skills: Arrays and objects, Functions
← Move with the arrows Turn with Up →