I need help with the ability to divide an unknown integer into a given number of even parts — or at least as even as they can be. The sum of the parts should be the original
You may get maximum integer coefficient as x/y
rounded down to integer and remainder with x%y
. Than simply break remainder into 1's and add those 1's to corresponding number of items:
const breakIntoParts = (num, parts) =>
[...Array(parts)].map((_,i) =>
0|(i < num%parts ? num/parts+1 : num/parts))
console.log(JSON.stringify(breakIntoParts(20, 6)));
.as-console-wrapper {min-height: 100%}