-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalculateGpa.ts
53 lines (44 loc) · 946 Bytes
/
calculateGpa.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
Given a set of letter grades, output the GPA (grade point average) of those grades.
Key and examples:
A = 4 grade points
A- = 3.7 grade points
B+ = 3.3 grade points
B = 3 grade points
B- = 2.7 grade points
C+ = 2.3 grade points
C = 2 grade points
C- = 1.7 grade points
D+ = 1.3 grade points
D = 1 grade point
D- = 0.7 grade points
F = 0 grade points
$ calculateGPA(['A'])
$ 4
$ calculateGPA(['F', 'F', 'F'])
$ 0
$ calculateGPA(['A', 'A-', 'B+', 'B', 'B-'])
$ 3.3
$ calculateGPA(['A', 'B+', 'C-', 'A'])
$ 3.3
*/
const GRADE_MAP = {
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,
} as const;
type Grade = keyof typeof GRADE_MAP;
export function calculateGPA(grades: Grade[]): number {
const gradeSum = grades.reduce((acc, grade) => acc + GRADE_MAP[grade], 0);
const roundedTo1Decimal = (gradeSum / grades.length).toFixed(1);
return Number(roundedTo1Decimal);
}