Skip to content

Instantly share code, notes, and snippets.

@igorsantos07
Created June 6, 2018 04:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igorsantos07/fafdbb7e2ae44172727474ba171d782b to your computer and use it in GitHub Desktop.
Save igorsantos07/fafdbb7e2ae44172727474ba171d782b to your computer and use it in GitHub Desktop.
Calculates unequal but proportional pieces of 100%
//used this through trial and error to find the correct proportions for a progress bar where one element is 10%, with 5 pieces
//based on https://stackoverflow.com/a/40094266/102960
function proportionalPieces(proportion, size) {
const pieces = [1]
for (let i = 1; i < size; i++) {
pieces[i] = pieces[i-1] * proportion
}
const sum = pieces.reduce((acc, piece) => acc + piece)
const correctionRatio = 100 / sum
for (i = 0; i < size; i++) {
pieces[i] *= correctionRatio
}
return pieces
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment