Skip to content

Instantly share code, notes, and snippets.

@gixxerblade
Created September 19, 2022 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gixxerblade/9df89433150cdbc9fabb77256d7d4fd3 to your computer and use it in GitHub Desktop.
Save gixxerblade/9df89433150cdbc9fabb77256d7d4fd3 to your computer and use it in GitHub Desktop.
cassidoo Interview question of the week 9-19-2022
const lookup = {
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,
}
type Grade = 'A' | 'A-' | 'B+' | 'B' | 'B-' | 'C+' | 'C' | 'C-' | 'D+' | 'D' | 'D-' | 'F';
const calculateGPA = (grades: Grade[]): number => grades.reduce((acc, curr) => (
Math.round((acc + lookup[curr] / grades.length) * 10) / 10
), 0)
console.log(calculateGPA(['A']));
console.log(calculateGPA(['F', 'F', 'F']));
console.log(calculateGPA(['A', 'A-', 'B+', 'B', 'B-']));
console.log(calculateGPA(['A', 'B+', 'C-', 'A']));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment