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.

Revisions

  1. GabrielModog created this gist Oct 18, 2024.
    15 changes: 15 additions & 0 deletions spiralize.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    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
    */