2018-10-10 22:03:03 +00:00
---
id: 594810f028c0303b75339ad8
2021-02-06 04:42:36 +00:00
title: Zig-zag matrix
2018-10-10 22:03:03 +00:00
challengeType: 5
2021-02-06 04:42:36 +00:00
forumTopicId: 302348
2021-01-13 02:31:00 +00:00
dashedName: zig-zag-matrix
2018-10-10 22:03:03 +00:00
---
2020-12-16 07:37:30 +00:00
# --description--
2021-02-06 04:42:36 +00:00
A 'zig-zag' array is a square arrangement of the first $N^2$ integers, where the numbers increase sequentially as you zig-zag along the array's [anti-diagonals ](https://en.wiktionary.org/wiki/antidiagonal ).
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
For example, for the input `5` , the following result should be produced:
< pre >
0 1 5 6 14
2 4 7 13 15
3 8 12 16 21
9 11 17 20 22
2020-12-16 07:37:30 +00:00
10 18 19 23 24
2021-02-06 04:42:36 +00:00
< / pre >
# --instructions--
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
Write a function that takes the size of the zig-zag matrix, and returns the corresponding matrix as two-dimensional array.
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
# --hints--
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
ZigZagMatrix should be a function.
2018-10-10 22:03:03 +00:00
```js
2020-12-16 07:37:30 +00:00
assert.equal(typeof ZigZagMatrix, 'function');
```
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
ZigZagMatrix should return array.
2020-12-16 07:37:30 +00:00
```js
assert.equal(typeof ZigZagMatrix(1), 'object');
2018-10-10 22:03:03 +00:00
```
2021-02-06 04:42:36 +00:00
ZigZagMatrix should return an array of nested arrays.
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
```js
assert.equal(typeof ZigZagMatrix(1)[0], 'object');
```
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
ZigZagMatrix(1) should return \[[0]].
2018-10-10 22:03:03 +00:00
```js
2020-12-16 07:37:30 +00:00
assert.deepEqual(ZigZagMatrix(1), zm1);
2018-10-10 22:03:03 +00:00
```
2021-02-06 04:42:36 +00:00
ZigZagMatrix(2) should return \[[0, 1], [2, 3]].
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
```js
assert.deepEqual(ZigZagMatrix(2), zm2);
```
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
ZigZagMatrix(5) should return specified matrix.
2018-10-10 22:03:03 +00:00
```js
2020-12-16 07:37:30 +00:00
assert.deepEqual(ZigZagMatrix(5), zm5);
2018-10-10 22:03:03 +00:00
```
2020-08-13 15:24:35 +00:00
2021-01-13 02:31:00 +00:00
# --seed--
## --after-user-code--
```js
const zm1 = [[0]];
const zm2 = [[0, 1], [2, 3]];
const zm5 = [
[0, 1, 5, 6, 14],
[2, 4, 7, 13, 15],
[3, 8, 12, 16, 21],
[9, 11, 17, 20, 22],
[10, 18, 19, 23, 24]
];
```
## --seed-contents--
```js
function ZigZagMatrix(n) {
return [[], []];
}
```
2020-12-16 07:37:30 +00:00
# --solutions--
2021-01-13 02:31:00 +00:00
```js
function ZigZagMatrix(n) {
const mtx = [];
for (let i = 0; i < n ; i + + ) {
mtx[i] = [];
}
let i = 1;
let j = 1;
for (let e = 0; e < n * n ; e + + ) {
mtx[i - 1][j - 1] = e;
if ((i + j) % 2 === 0) {
// Even stripes
if (j < n ) j + + ;
else i += 2;
if (i > 1) i--;
} else {
// Odd stripes
if (i < n ) i + + ;
else j += 2;
if (j > 1) j--;
}
}
return mtx;
}
```