2018-12-30 19:05:56 +05:30
---
id: 5a23c84252665b21eecc801c
title: Spiral matrix
challengeType: 5
2019-08-05 09:17:33 -07:00
forumTopicId: 302321
2018-12-30 19:05:56 +05:30
---
## Description
2020-03-30 11:23:18 -05:00
2018-12-30 19:05:56 +05:30
<section id='description'>
2019-07-18 17:32:12 +02:00
2018-12-30 19:05:56 +05:30
Produce a spiral array.
2020-03-30 11:23:18 -05:00
A <i>spiral array</i> is a square arrangement of the first N<sup>2</sup> natural numbers, where the numbers increase sequentially as you go around the edges of the array spiraling inwards.
For example, given <b>5</b>, produce this array:
2018-12-30 19:05:56 +05:30
<pre>
0 1 2 3 4
15 16 17 18 5
14 23 24 19 6
13 22 21 20 7
12 11 10 9 8
</pre>
</section>
## Instructions
2020-03-30 11:23:18 -05:00
2018-12-30 19:05:56 +05:30
<section id='instructions'>
</section>
## Tests
2020-03-30 11:23:18 -05:00
2018-12-30 19:05:56 +05:30
<section id='tests'>
2020-03-30 11:23:18 -05:00
```yml
2018-12-30 19:05:56 +05:30
tests:
- text: <code>spiralArray</code> should be a function.
2020-03-30 11:23:18 -05:00
testString: assert(typeof spiralArray=='function');
2019-03-08 18:00:54 +09:00
- text: <code>spiralArray(3)</code> should return an array.
2020-03-30 11:23:18 -05:00
testString: assert(Array.isArray(spiralArray(3)));
2018-12-30 19:05:56 +05:30
- text: <code>spiralArray(3)</code> should return <code>[[0, 1, 2],[7, 8, 3],[6, 5, 4]]</code>.
2020-03-30 11:23:18 -05:00
testString: assert.deepEqual(spiralArray(3), [[0, 1, 2], [7, 8, 3], [6, 5, 4]]);
2018-12-30 19:05:56 +05:30
- text: <code>spiralArray(4)</code> should return <code>[[0, 1, 2, 3],[11, 12, 13, 4],[10, 15, 14, 5],[9, 8, 7, 6]]</code>.
2020-03-30 11:23:18 -05:00
testString: assert.deepEqual(spiralArray(4), [[0, 1, 2, 3], [11, 12, 13, 4], [10, 15, 14, 5], [9, 8, 7, 6]]);
2018-12-30 19:05:56 +05:30
- text: <code>spiralArray(5)</code> should return <code>[[0, 1, 2, 3, 4],[15, 16, 17, 18, 5],[14, 23, 24, 19, 6],[13, 22, 21, 20, 7],[12, 11, 10, 9, 8]]</code>.
2020-03-30 11:23:18 -05:00
testString: assert.deepEqual(spiralArray(5), [[0, 1, 2, 3, 4], [15, 16, 17, 18, 5], [14, 23, 24, 19, 6], [13, 22, 21, 20, 7], [12, 11, 10, 9, 8]]);
2018-12-30 19:05:56 +05:30
```
</section>
## Challenge Seed
2020-03-30 11:23:18 -05:00
2018-12-30 19:05:56 +05:30
<section id='challengeSeed'>
2019-07-18 17:32:12 +02:00
2018-12-30 19:05:56 +05:30
<div id='js-seed'>
```js
2019-03-08 18:00:54 +09:00
function spiralArray(n) {
2018-12-30 19:05:56 +05:30
// Good luck!
}
```
</div>
</section>
## Solution
2020-03-30 11:23:18 -05:00
2018-12-30 19:05:56 +05:30
<section id='solution'>
```js
2019-03-08 18:00:54 +09:00
function spiralArray(n) {
2020-03-30 11:23:18 -05:00
var arr = Array(n),
x = 0,
y = n,
total = n * n--,
dx = 1,
dy = 0,
i = 0,
j = 0;
while (y) arr[--y] = [];
while (i < total) {
arr[y][x] = i++;
x += dx;
y += dy;
if (++j == n) {
if (dy < 0) {
x++;
y++;
n -= 2;
}
j = dx;
dx = -dy;
dy = j;
j = 0;
2018-12-30 19:05:56 +05:30
}
2020-03-30 11:23:18 -05:00
}
return arr;
2018-12-30 19:05:56 +05:30
}
```
2019-07-18 17:32:12 +02:00
</section>