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
Extremely simple solution:
const splitInteger = (number, parts) => {
const remainder = number % parts
const baseValue = (number - remainder) / parts
return Array(parts).fill(baseValue).fill(baseValue + 1, parts - remainder)
}
To order from largest to smallest:
const splitInteger = (number, parts) => {
const remainder = number % parts
const baseValue = (number - remainder) / parts
return Array(parts).fill(baseValue).fill(baseValue + 1, 0, remainder)
}
Now as an npm module: split-integer