Skip to content

Commit

Permalink
move gpaToLetterGrade func to calculateGrades
Browse files Browse the repository at this point in the history
  • Loading branch information
TyHil committed Oct 29, 2024
1 parent 8a5c65e commit ab3cbff
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 33 deletions.
17 changes: 1 addition & 16 deletions src/components/CourseOverview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,6 @@ import { TRENDS_URL } from '~data/config';
import type { GenericFetchedData, GradesType } from '~pages';
import { type SearchQuery, searchQueryLabel } from '~utils/SearchQuery';

const gpaToLetterGrade = (gpa: number): string => {
if (gpa >= 4.0) return 'A';
if (gpa >= 3.67) return 'A-';
if (gpa >= 3.33) return 'B+';
if (gpa >= 3.0) return 'B';
if (gpa >= 2.67) return 'B-';
if (gpa >= 2.33) return 'C+';
if (gpa >= 2.0) return 'C';
if (gpa >= 1.67) return 'C-';
if (gpa >= 1.33) return 'D+';
if (gpa >= 1.0) return 'D';
if (gpa >= 0.67) return 'D-';
return 'F';
};

type CourseOverviewProps = {
course: SearchQuery;
grades: GenericFetchedData<GradesType>;
Expand All @@ -32,7 +17,7 @@ const CourseOverview = ({ course, grades }: CourseOverviewProps) => {
</p>
{grades.state === 'done' && (
<p className="text-lg font-semibold text-center">
{'Overall grade: ' + gpaToLetterGrade(grades.data.gpa)}
{'Overall grade: ' + grades.data.letter_grade}
</p>
)}
<a
Expand Down
19 changes: 2 additions & 17 deletions src/components/SearchResultsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,6 @@ import {
searchQueryLabel,
} from '~utils/SearchQuery';

const gpaToLetterGrade = (gpa: number): string => {
if (gpa >= 4.0) return 'A';
if (gpa >= 3.67) return 'A-';
if (gpa >= 3.33) return 'B+';
if (gpa >= 3.0) return 'B';
if (gpa >= 2.67) return 'B-';
if (gpa >= 2.33) return 'C+';
if (gpa >= 2.0) return 'C';
if (gpa >= 1.67) return 'C-';
if (gpa >= 1.33) return 'D+';
if (gpa >= 1.0) return 'D';
if (gpa >= 0.67) return 'D-';
return 'F';
};

type RowProps = {
course: SearchQuery;
grades: GenericFetchedData<GradesType>;
Expand Down Expand Up @@ -164,7 +149,7 @@ function Row({ course, grades, backupGrades, rmp, setPage }: RowProps) {
backgroundColor: gpaToColor(backupGrades.data.gpa),
}}
>
{gpaToLetterGrade(backupGrades.data.gpa)}
{backupGrades.data.letter_grade}
</Typography>
</Badge>
</Tooltip>
Expand All @@ -186,7 +171,7 @@ function Row({ course, grades, backupGrades, rmp, setPage }: RowProps) {
className="text-base text-black rounded-full px-5 py-2 inline"
sx={{ backgroundColor: gpaToColor(grades.data.gpa) }}
>
{gpaToLetterGrade(grades.data.gpa)}
{grades.data.letter_grade}
</Typography>
</Tooltip>
)) ||
Expand Down
18 changes: 18 additions & 0 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@ export type GenericFetchedData<T> =
| GenericFetchedDataLoading
| GenericFetchedDataDone<T>;

const gpaToLetterGrade = (gpa: number): string => {
if (gpa >= 4.0) return 'A';
if (gpa >= 3.67) return 'A-';
if (gpa >= 3.33) return 'B+';
if (gpa >= 3.0) return 'B';
if (gpa >= 2.67) return 'B-';
if (gpa >= 2.33) return 'C+';
if (gpa >= 2.0) return 'C';
if (gpa >= 1.67) return 'C-';
if (gpa >= 1.33) return 'D+';
if (gpa >= 1.0) return 'D';
if (gpa >= 0.67) return 'D-';
return 'F';
};

//Find GPA, total, and grade_distribution based on including some set of semesters
function calculateGrades(grades: GradesData, academicSessions?: string[]) {
let grade_distribution = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
Expand Down Expand Up @@ -68,11 +83,13 @@ function calculateGrades(grades: GradesData, academicSessions?: string[]) {
) /
(total - grade_distribution[grade_distribution.length - 1]);
}
const letter_grade = gpaToLetterGrade(gpa);

return {
gpa: gpa,
total: total,
grade_distribution: grade_distribution,
letter_grade: letter_grade,
};
}
type GradesData = {
Expand All @@ -83,6 +100,7 @@ export type GradesType = {
gpa: number;
total: number;
grade_distribution: number[];
letter_grade: string;
grades: GradesData;
};
//Fetch grades by academic session from nebula api
Expand Down

0 comments on commit ab3cbff

Please sign in to comment.