Skip to content

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]
See also  Birthday Cake Candles - Hackerrank Challenge - Java Solution

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.