All files / server/src/features/assignment/padg/utils calculateHappiness.ts

100% Statements 10/10
100% Branches 2/2
100% Functions 2/2
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22    45x       206x   206x 1082x 13198x   1082x 1082x 1082x 934x       206x    
import { PadgRandomAssignResult, Group } from "server/types/assignmentTypes";
 
export const calculateHappiness = (
  assignResults: PadgRandomAssignResult[],
  groups: Group[],
): number => {
  let happiness = 0;
 
  for (const assignResult of assignResults) {
    const groupIndex = groups.findIndex(
      (group) => group.id === assignResult.id,
    );
    let index = groups[groupIndex].pref.indexOf(assignResult.assignment);
    index = index + 1;
    if (index > 0) {
      happiness = happiness + 1 / index;
    }
  }
 
  return Math.round(((happiness / groups.length) * 10000) / 100);
};