Skip to content

Instantly share code, notes, and snippets.

@Camvillard
Last active September 19, 2022 13:40
Show Gist options
  • Save Camvillard/b5a189bd76f33d8cb770d127a5c7b659 to your computer and use it in GitHub Desktop.
Save Camvillard/b5a189bd76f33d8cb770d127a5c7b659 to your computer and use it in GitHub Desktop.
rdv with cassidoo - Interview question of the week (2022-09-19)
enum Grade {
A = 4,
"A-" = 3.7,
"B+" = 3.3,
B = 3,
"B-" = 2.7,
"C+" = 2.3,
C = 2,
"C-" = 1.7,
"D+" = 1.3,
D = 1,
"D-" = 0.7,
F = 0,
}
const calculateGPA = (grades: Grade[], rounding = 1) => {
if (!Array.isArray(grades) || !grades.length) {
return console.error("cannot calculate gpa");
}
const max = grades.reduce((prev, current) => {
return prev + +Grade[current];
}, 0);
return +(max / grades.length).toFixed(rounding);
};
calculateGPA([Grade.A]);
// 4
calculateGPA([Grade.F, Grade.F, Grade.F]);
// 0
calculateGPA([Grade.A, Grade["A-"], Grade["B+"], Grade.B, Grade["B-"]]);
// 3.3
calculateGPA([Grade.A, Grade["B+"], Grade.B, Grade["B-"]], 2);
// 3.25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment