Skip to content

Instantly share code, notes, and snippets.

@GabrielModog
Created October 18, 2024 04:56
Show Gist options
  • Save GabrielModog/c3525b71ab6d589af7994990c10d1e3d to your computer and use it in GitHub Desktop.
Save GabrielModog/c3525b71ab6d589af7994990c10d1e3d to your computer and use it in GitHub Desktop.
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