Created
May 31, 2022 03:33
-
-
Save jonrandy/b72b5897966a0b1d87adae092b6b02d3 to your computer and use it in GitHub Desktop.
Split integer by ratios
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function allocateInt(amount, ratios) { | |
if (ratios.length == 1) { | |
return [amount] | |
} else { | |
const rs = [...ratios] | |
const divisor = rs.reduce((a,i)=>a+i) | |
const thisAmount = Math.round(amount*rs.shift()/divisor) | |
return [ | |
thisAmount, | |
...allocateInt(amount-thisAmount, rs) | |
] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment