Created
October 18, 2024 04:56
-
-
Save GabrielModog/c3525b71ab6d589af7994990c10d1e3d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function spiralize(n) { | |
| return Array(n).fill(Array(n).fill(0)).map((i, row) => i.map((j, col) => { | |
| const north = row < n / 2 && row % 2 === 0 && col >= row - 1 && col <= n - row - 1 | |
| const east = (n-col) % 2 === 1 && row <= col && row > n - col - 1 | |
| const south = (n-row) % 2 === 1 && col < row && col > n - row - 1 | |
| const west = col % 2 === 0 && row < n - col && row > col + 1 | |
| const result = north || east || south || west | |
| return Number(result) | |
| })) | |
| } | |
| /** | |
| * Number(result) | |
| * false and true in JS can be read as 0 or 1 | |
| * so the class Number can turn boolean values into valid a number | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment