[+] Create method to calculate GPA
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
/**
|
/**
|
||||||
* This is an utility class to calculate GPA.
|
* This is an utility class to calculate GPA.
|
||||||
*/
|
*/
|
||||||
|
import {Course} from '@/components/app/app';
|
||||||
|
|
||||||
export class GPAUtils
|
export class GPAUtils
|
||||||
{
|
{
|
||||||
// [[Min score, Letter grade, Base GPA], ...]
|
// [[Min score, Letter grade, Base GPA], ...]
|
||||||
@@ -23,4 +25,49 @@ export class GPAUtils
|
|||||||
public static MIN = 0;
|
public static MIN = 0;
|
||||||
public static LETTER = 1;
|
public static LETTER = 1;
|
||||||
public static GPA = 2;
|
public static GPA = 2;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculate GPA for a list of couses
|
||||||
|
*
|
||||||
|
* @param coursesOriginal List of courses
|
||||||
|
*/
|
||||||
|
public static gpa(coursesOriginal: Course[]): number
|
||||||
|
{
|
||||||
|
// Clone array
|
||||||
|
let courses: Course[] = [];
|
||||||
|
|
||||||
|
// Remove all courses that does not have a grade
|
||||||
|
coursesOriginal.forEach(course =>
|
||||||
|
{
|
||||||
|
if (course.numericGrade != null)
|
||||||
|
{
|
||||||
|
courses.push(course);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// If no course have grade, return -1
|
||||||
|
if (courses.length == 0)
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Count total GPA
|
||||||
|
let totalGPA = 0;
|
||||||
|
courses.forEach(course =>
|
||||||
|
{
|
||||||
|
// Find the GPA for this course. TODO: Remove course that does not have level
|
||||||
|
this.SCALE.forEach(scale =>
|
||||||
|
{
|
||||||
|
// Letter grades are the same
|
||||||
|
if (scale[this.LETTER] == course.letterGrade)
|
||||||
|
{
|
||||||
|
totalGPA += <number> scale[this.GPA] + course.scaleUp;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
// Get average GPA
|
||||||
|
return totalGPA / courses.length;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user