Created
March 17, 2022 23:00
-
-
Save lyatziv/c0c2328823dc0523e5dd1edde7bbc7f9 to your computer and use it in GitHub Desktop.
Get page numbering for booklet printing
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
const makePamphlet = (suspectInput) => { | |
const input = Boolean(suspectInput % 2) ? suspectInput + 1 : suspectInput; | |
const evenPages = [ | |
...Array(input / 2) | |
.fill(0) | |
.map((_el, idx) => (idx + 1) * 2), | |
].reverse(); | |
const oddPages = [ | |
...Array(input) | |
.fill(0) | |
.map((_el, idx) => idx % 2 && idx) | |
.filter((e) => e), | |
]; | |
return [ | |
...Array(input/2) | |
.fill(0) | |
.map((_el, idx) => { | |
const output = (Boolean(idx%2)) | |
? [`${evenPages.pop()}`, `${oddPages.pop()}`] // second [2, 15] | |
: [`${evenPages.shift()}`, `${oddPages.shift()}`] // first [16,1] | |
return output | |
}) | |
].flatMap(p => p).map(n => parseInt(n)); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment