You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
756 B
30 lines
756 B
/**
|
|
* A few useful function for dealing with majority judgment
|
|
*/
|
|
|
|
import { MeritProfileInterface } from './type';
|
|
|
|
/**
|
|
* Return the index corresponding to the majority grade
|
|
*/
|
|
export const getMajorityGrade = (profile: MeritProfileInterface): number => {
|
|
const indices = Object.keys(profile);
|
|
const numVotes = Object.values(profile).reduce((a, b) => a + b, 0);
|
|
let majorityGrade = indices[0];
|
|
let accBefore = 0;
|
|
let isBefore = true;
|
|
|
|
for (const value of indices) {
|
|
if (isBefore) {
|
|
accBefore += profile[value];
|
|
}
|
|
if (isBefore && accBefore > numVotes / 2) {
|
|
majorityGrade = value;
|
|
accBefore -= profile[value];
|
|
isBefore = false;
|
|
}
|
|
}
|
|
const value = indices.indexOf(majorityGrade);
|
|
return value;
|
|
};
|