Divide an integer into evenly distributed groups with an ES6 generator
August 27, 2017 Leave a comment
Say you’d like to divide an integer, e.g. 20, into equal parts of 3 and distribute any remainder equally across the groups. The result of such an operation would be the following 3 integers:
7,7,6
20 can be divided into 3 equal parts of 6 and we have a remainder of 20 – 6 * 3 = 2. 2 is then added as 1 and 1 to the first two groups of 6. The result is a more or less equal distribution of the starting integer.
The following ES6 generator function will perform just that: