Created
June 6, 2018 04:19
-
-
Save igorsantos07/fafdbb7e2ae44172727474ba171d782b to your computer and use it in GitHub Desktop.
Calculates unequal but proportional pieces of 100%
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
//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