EXERCISE · AlgoCasts

Spiral matrix

Write a function that accepts an integer N and returns a NxN spiral matrix.

4 testsAlgoCastsGPL-3.0exercise 12 of 26

Directions

Directions

Write a function that accepts an integer N and returns a NxN spiral matrix.

Examples#

JS
matrix(2)  [[1, 2],  [4, 3]]matrix(3)  [[1, 2, 3],  [8, 9, 4],  [7, 6, 5]] matrix(4)  [[1,   2,  3, 4],  [12, 13, 14, 5],  [11, 16, 15, 6],  [10,  9,  8, 7]]

Try it first#

Copy the stub, make the 4 tests pass on your machine, then open the solution. The reveal is remembered per page in this browser.

JSexercises/matrix/index.js
function matrix(n) {} module.exports = matrix;

See also