mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-27 17:46:05 -05:00
FIX: use an algorithm that doesn't re-order percentages when rounding them up to 100% in single choice polls
This commit is contained in:
parent
434deb1bd3
commit
68a44a1d59
2 changed files with 11 additions and 11 deletions
|
@ -5,17 +5,16 @@ export default Em.Component.extend({
|
||||||
tagName: "ul",
|
tagName: "ul",
|
||||||
classNames: ["results"],
|
classNames: ["results"],
|
||||||
|
|
||||||
@computed("poll.voters", "poll.options.[]", "poll.type")
|
@computed("poll.voters", "poll.type", "poll.options.[]")
|
||||||
options() {
|
options(voters, type) {
|
||||||
const options = this.get("poll.options");
|
const options = this.get("poll.options");
|
||||||
const voters = this.get("poll.voters");
|
|
||||||
|
|
||||||
let percentages = voters === 0 ?
|
let percentages = voters === 0 ?
|
||||||
Array(options.length).fill(0) :
|
Array(options.length).fill(0) :
|
||||||
_.map(options, o => 100 * o.get("votes") / voters);
|
_.map(options, o => 100 * o.get("votes") / voters);
|
||||||
|
|
||||||
// properly round percentages
|
// properly round percentages
|
||||||
if (this.get("poll.type") === "multiple") {
|
if (type === "multiple") {
|
||||||
// when the poll is multiple choices, just "round down"
|
// when the poll is multiple choices, just "round down"
|
||||||
percentages = percentages.map(p => Math.floor(p));
|
percentages = percentages.map(p => Math.floor(p));
|
||||||
} else {
|
} else {
|
||||||
|
@ -34,7 +33,7 @@ export default Em.Component.extend({
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
return this.get("poll.options");
|
return options;
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
// stolen from http://stackoverflow.com/a/13485888/11983
|
// stolen from http://stackoverflow.com/a/13484088/11983
|
||||||
export default (percentages) => {
|
export default (percentages) => {
|
||||||
const off = 100 - _.reduce(percentages, (acc, x) => acc + Math.round(x), 0);
|
const sumOfDecimals = Math.ceil(percentages.map(a => a % 1).reduce((a, b) => a + b));
|
||||||
return _.chain(percentages)
|
// compensate error by adding 1 to the first n items
|
||||||
.sortBy(x => Math.round(x) - x)
|
for (let i = 0; i < sumOfDecimals; i++) {
|
||||||
.map((x, i) => Math.round(x) + (off > i) - (i >= (percentages.length + off)))
|
percentages[i] = ++percentages[i];
|
||||||
.value();
|
}
|
||||||
|
return percentages.map(a => Math.floor(a));
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue