sendou.ink/lib/plus.ts
2021-02-20 21:14:57 +02:00

25 lines
757 B
TypeScript

import { PlusRegion } from "@prisma/client";
export const getPercentageFromCounts = (
countsNA: number[],
countsEU: number[],
votedUserRegion: PlusRegion
) => {
const sameRegionArr = votedUserRegion === "NA" ? countsNA : countsEU;
const otherRegionArr = votedUserRegion === "NA" ? countsEU : countsNA;
const sameSum =
sameRegionArr[0] * -2 +
sameRegionArr[1] * -1 +
sameRegionArr[2] * 1 +
sameRegionArr[3] * 2;
const sameVoterCount = sameRegionArr.reduce((acc, cur) => acc + cur, 0);
const otherSum = otherRegionArr[1] * -1 + otherRegionArr[2] * 1;
const otherVoterCount = otherRegionArr.reduce((acc, cur) => acc + cur, 0);
return (
((sameSum / sameVoterCount + otherSum / otherVoterCount + 3) / 6) * 100
);
};