mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-27 17:46:05 -05:00
FIX: round down percentages when using multiple choices polls
This commit is contained in:
parent
bde05fd334
commit
4f25278b39
1 changed files with 8 additions and 3 deletions
|
@ -5,16 +5,21 @@ export default Em.Component.extend({
|
|||
tagName: "ul",
|
||||
classNames: ["results"],
|
||||
|
||||
@computed("poll.voters", "poll.options.[]", "poll.isMultiple")
|
||||
@computed("poll.voters", "poll.options.[]", "poll.type")
|
||||
options() {
|
||||
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);
|
||||
|
||||
// fix percentages to add up to 100 when the poll is single choice only
|
||||
if (!this.get("poll.isMultiple")) {
|
||||
// properly round percentages
|
||||
if (this.get("poll.type") === "multiple") {
|
||||
// when the poll is multiple choices, just "round down"
|
||||
percentages = percentages.map(p => Math.floor(p));
|
||||
} else {
|
||||
// when the poll is single choice, adds up to 100%
|
||||
percentages = evenRound(percentages);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue