mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-31 19:00:32 -04:00
1.8 KiB
1.8 KiB
id, title, challengeType, forumTopicId, dashedName
| id | title | challengeType | forumTopicId | dashedName |
|---|---|---|---|---|
| 5a23c84252665b21eecc801c | Matriz espiral | 1 | 302321 | spiral-matrix |
--description--
Producir un arreglo espiral. Un spiral array es una disposición cuadrada de primer N2 números naturales, donde los números incrementan secuencialmente por todas las esquinas del arreglo espiral interno. Por ejemplo, dado 5, produce este arreglo:
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
--hints--
spiralArray debería ser una función.
assert(typeof spiralArray == 'function');
spiralArray(3) debería devolver un arreglo.
assert(Array.isArray(spiralArray(3)));
spiralArray(3) debería devolver [[0, 1, 2],[7, 8, 3],[6, 5, 4]].
assert.deepEqual(spiralArray(3), [
[0, 1, 2],
[7, 8, 3],
[6, 5, 4]
]);
spiralArray(4) debería devolver [[0, 1, 2, 3],[11, 12, 13, 4],[10, 15, 14, 5],[9, 8, 7, 6]].
assert.deepEqual(spiralArray(4), [
[0, 1, 2, 3],
[11, 12, 13, 4],
[10, 15, 14, 5],
[9, 8, 7, 6]
]);
spiralArray(5) debería devolver [[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]].
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]
]);
--seed--
--seed-contents--
function spiralArray(n) {
}
--solutions--
function spiralArray(n) {
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;
}
}
return arr;
}