-
Notifications
You must be signed in to change notification settings - Fork 0
/
spiralize.js
36 lines (35 loc) · 1.31 KB
/
spiralize.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//we make an array filled with booleans, then we turn those values into numbers
// false turns to 0, true turns to 1
const spiralize = (size) => {
//make bidimensional array
let spiral = Array(size)
.fill(Array(size).fill())
.map((i, row) =>
i.map((j, column) => {
const isEven = (x) => x % 2 === 0;
const isOdd = (x) => x % 2 === 1;
const northArea =
row < size / 2 && //deal with middle square
isEven(row) && //jump row
column >= row - 1 && //diagonal cut cinit to rend
column <= size - row - 1; //diagonal cut cend to rend
const eastArea =
isOdd(size - column) && //jump column
row <= column && //diagonal cut cinit to cend
row > size - column - 1; //diagonal cut rend to cend
const southArea =
isOdd(size - row) && //jump row
column < row && //diagonal cut cinit to cend
column > size - row - 1; //diagonal cut cend to rinit
const westArea =
isEven(column) && //jump column
row < size - column &&
row > column + 1; //diagonal cut
const drawSpiral = northArea || eastArea || southArea || westArea;
//turn boolean into number
return Number(drawSpiral);
})
);
return spiral;
};
console.log(spiralize(8));