Skip to content

Instantly share code, notes, and snippets.

@jonrandy
Created May 31, 2022 03:33
Show Gist options
  • Save jonrandy/b72b5897966a0b1d87adae092b6b02d3 to your computer and use it in GitHub Desktop.
Save jonrandy/b72b5897966a0b1d87adae092b6b02d3 to your computer and use it in GitHub Desktop.
Split integer by ratios
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