Dev Helpers
← Back

Allocate whole units fairly

Allocate whole units fairly using JavaScript.

Open this example in the interactive library

Source code

const n = 17,
  groups = 4;
console.log(
  Array.from(
    { length: groups },
    (_, i) => Math.floor(n / groups) + (i < n % groups ? 1 : 0),
  ),
);

Sample output

[ 5, 4, 4, 4 ]