From 4ce99c998b6b77fce4b2590c17b921b3b76faa38 Mon Sep 17 00:00:00 2001 From: rizka10 Date: Thu, 3 Nov 2016 11:11:17 +0200 Subject: [PATCH] Improve summing poll percentages to 100 My first JavaScript! I have little experience with C++ and even less with Java, but that was enough to figure out a way to solve the task. The solution is rather good, but there may be better ways. I'm going to start a pull request. Even if it gets rejected, an expert can use the idea. NOTE: The code needs some serious testing before potential merging. I did some testing and it worked, but don't trust in my skills. --- .../assets/javascripts/lib/even-round.js.es6 | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/plugins/poll/assets/javascripts/lib/even-round.js.es6 b/plugins/poll/assets/javascripts/lib/even-round.js.es6 index 0395f1f16a6..5446c874c58 100644 --- a/plugins/poll/assets/javascripts/lib/even-round.js.es6 +++ b/plugins/poll/assets/javascripts/lib/even-round.js.es6 @@ -1,17 +1,29 @@ -// stolen from http://stackoverflow.com/a/13484088/11983 +// works as described on http://stackoverflow.com/a/13483710 function sumsUpTo100(percentages) { return percentages.map(p => Math.floor(p)).reduce((a, b) => a + b) === 100; } export default (percentages) => { - const sumOfDecimals = Math.ceil(percentages.map(a => a % 1).reduce((a, b) => a + b)); - // compensate error by adding 1 to the first n "non-zero" items - for (let i = 0, max = percentages.length; i < sumOfDecimals && i < max; i++) { - if (percentages[i] > 0) { - percentages[i] = ++percentages[i]; - // quit early when there is a rounding issue - if (sumsUpTo100(percentages)) break; + var decimals = percentages.map(a => a % 1); + const sumOfDecimals = Math.ceil(decimals.reduce((a, b) => a + b)); + // compensate error by adding 1 to n items with the greatest decimal part + for (let i = 0, max = decimals.length; i < sumOfDecimals && i < max; i++) { + // find the greatest item in the decimals array, set it to 0, + // and increase the corresponding item in the percentages array by 1 + let greatest = 0; + let index = 0; + for (let j=0; j < decimals.length; j++) { + if (decimals[j] > greatest) { + index = j; + greatest = decimals[j]; + } } + ++percentages[index]; + decimals[index] = 0; + // quit early when there is a rounding issue + if (sumsUpTo100(percentages)) break; } + return percentages.map(p => Math.floor(p)); }; +