diff --git a/plugins/poll/assets/javascripts/components/poll-results-standard.js.es6 b/plugins/poll/assets/javascripts/components/poll-results-standard.js.es6 index 17dece580..0e4421504 100644 --- a/plugins/poll/assets/javascripts/components/poll-results-standard.js.es6 +++ b/plugins/poll/assets/javascripts/components/poll-results-standard.js.es6 @@ -5,17 +5,16 @@ export default Em.Component.extend({ tagName: "ul", classNames: ["results"], - @computed("poll.voters", "poll.options.[]", "poll.type") - options() { + @computed("poll.voters", "poll.type", "poll.options.[]") + options(voters, type) { const options = this.get("poll.options"); - const voters = this.get("poll.voters"); let percentages = voters === 0 ? Array(options.length).fill(0) : _.map(options, o => 100 * o.get("votes") / voters); // properly round percentages - if (this.get("poll.type") === "multiple") { + if (type === "multiple") { // when the poll is multiple choices, just "round down" percentages = percentages.map(p => Math.floor(p)); } else { @@ -34,7 +33,7 @@ export default Em.Component.extend({ }); }); - return this.get("poll.options"); + return options; } }); diff --git a/plugins/poll/assets/javascripts/lib/even-round.js.es6 b/plugins/poll/assets/javascripts/lib/even-round.js.es6 index 0a963aac6..541f6e27f 100644 --- a/plugins/poll/assets/javascripts/lib/even-round.js.es6 +++ b/plugins/poll/assets/javascripts/lib/even-round.js.es6 @@ -1,8 +1,9 @@ -// stolen from http://stackoverflow.com/a/13485888/11983 +// stolen from http://stackoverflow.com/a/13484088/11983 export default (percentages) => { - const off = 100 - _.reduce(percentages, (acc, x) => acc + Math.round(x), 0); - return _.chain(percentages) - .sortBy(x => Math.round(x) - x) - .map((x, i) => Math.round(x) + (off > i) - (i >= (percentages.length + off))) - .value(); + const sumOfDecimals = Math.ceil(percentages.map(a => a % 1).reduce((a, b) => a + b)); + // compensate error by adding 1 to the first n items + for (let i = 0; i < sumOfDecimals; i++) { + percentages[i] = ++percentages[i]; + } + return percentages.map(a => Math.floor(a)); };