Divide a number into X parts in JavaScript
This is the code snippet to divide a number n into x parts so that the sum of all parts will be equal to the number n.
Advertisements
const breakIntoParts = (num, parts) => [...Array(parts)].map((_,i) => 0|num/parts+(i < num%parts)) console.log(JSON.stringify(breakIntoParts(100, 7))); //[15,15,14,14,14,14,14]