Created a score calculator

This commit is contained in:
Tiago Ribeiro
2023-06-05 14:04:58 +01:00
parent 1e8e95da34
commit 9a7853bd05

61
src/utils/score.ts Normal file
View File

@@ -0,0 +1,61 @@
type Type = "academic" | "general";
const readingGeneralMarking: {[key: number]: number} = {
100: 9,
97.5: 8.5,
92.5: 8,
90: 7.5,
85: 7,
80: 6.5,
75: 6,
67.5: 5.5,
57.5: 5,
45.5: 4.5,
37.5: 4,
30: 3.5,
22.5: 3,
15: 2.5,
};
const academicMarking: {[key: number]: number} = {
97.5: 9,
92.5: 8.5,
87.5: 8,
80: 7.5,
75: 7,
65: 6.5,
57.5: 6,
45: 5.5,
40: 5,
32.5: 4.5,
25: 4,
20: 3.5,
15: 3,
10: 2.5,
};
const moduleMarkings: {[key in "reading" | "listening"]: {[key in Type]: {[key: number]: number}}} = {
reading: {
academic: academicMarking,
general: readingGeneralMarking,
},
listening: {
academic: academicMarking,
general: academicMarking,
},
};
export const calculateBandScore = (correct: number, total: number, module: "reading" | "listening", type: Type) => {
const marking = moduleMarkings[module][type];
const percentage = (correct * 100) / total;
for (const value of Object.keys(marking)
.map((x) => parseFloat(x))
.sort((a, b) => b - a)) {
if (percentage >= value) {
return marking[value];
}
}
return 0;
};