Last active
March 7, 2019 15:32
-
-
Save eugenserbanescu/9b90581b1c979d0eb7afc0e9789f87ec to your computer and use it in GitHub Desktop.
This file contains 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 input = [ | |
[ 1, 2, 3 ], | |
[ 4, 5 ], | |
[ 6, 7, 8, 9 ], | |
[ 10 ], | |
[ 11, 12 ], | |
[ 13, 14, 15, 16, 17 ] | |
] | |
const outputLimit = 12 | |
const chunkLimit = 2 | |
function fill(input, outputLimit, chunkLimit, output=[], innerIndex=0) { | |
const maxIndex = outputLimit - output.length | |
const toRemove = [] | |
input.forEach((list, i) => { | |
if(i >= maxIndex) return | |
const item = list[innerIndex] | |
if(item === undefined && list.length <= innerIndex) { | |
toRemove.push(i) | |
} else { | |
output.push(item) | |
} | |
}) | |
input = input.filter((_, index) => toRemove.indexOf(index) === -1) | |
innerIndex++ | |
if(output.length === outputLimit || input.length === 0 || innerIndex === chunkLimit) { | |
return output | |
} else { | |
return fill(input, outputLimit, chunkLimit, output, innerIndex) | |
} | |
} | |
console.log(input) | |
console.log(fill(input, outputLimit, chunkLimit)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment