@@ -77,9 +77,8 @@ function main_page () {
7777 const $grade_rows = $ ( '#quickLookup table.grid' ) . find ( 'tr' ) ;
7878 let s1col = 0 ;
7979 let s2col = 0 ;
80- let current_semester = 1 ;
8180 let current_term = "" ;
82-
81+ let attendance_href = "" ;
8382 if ( $grade_rows . eq ( 1 ) . html ( ) . match ( "S2" ) != null ) {
8483 second_semester = true ;
8584 let curr = 0 ;
@@ -128,8 +127,8 @@ function main_page () {
128127 }
129128 }
130129 }
131- if ( $grade_rows . eq ( $grade_rows . length - 1 ) . find ( 'a[href*="attendancedates"]' ) [ 0 ] . href && $grade_rows . eq ( $grade_rows . length - 1 ) . find ( 'a[href*="attendancedates"]' ) [ 0 ] . href . includes ( "term" ) ) {
132- current_term = $grade_rows . eq ( $grade_rows . length - 1 ) . find ( 'a[href*="attendancedates"]' ) [ 0 ] . href . split ( "term=" ) [ 1 ] . split ( "&" ) [ 0 ] ;
130+ if ( ( attendance_href = $grade_rows . eq ( $grade_rows . length - 1 ) ? .find ( 'a[href*="attendancedates"]' ) ?. [ 0 ] ? .href ) ) { // Check that attendance_href exists and if it does, run the next line.
131+ current_term = new URL ( attendance_href ) . searchParams . get ( "term" ) ;
133132 }
134133 $ ( "table[border='0'][cellpadding='3'][cellspacing='1'][width='100%']" ) . prepend ( `<tr><td align="center">Current Semester GPA (${ second_semester ? 'S2' : 'S1' } ): ${ calculate_gpa ( courses ) } </td></tr>` ) ;
135134
@@ -147,10 +146,10 @@ function main_page () {
147146 if ( element_list . length > 2 ) {
148147 for ( let i = 2 ; i < element_list . length ; i ++ ) {
149148 const $prev_course = element_list [ i ] ;
150- if ( $prev_course . innerText . trim ( ) === ( "S2" ) ) {
149+ if ( $prev_course ? .innerText ? .trim ( ) === "S2" ) {
151150 break ;
152151 }
153- if ( $prev_course && $prev_course . getElementsByTagName ( "td" ) . length > 1 ) {
152+ if ( $prev_course ? .getElementsByTagName ( "td" ) . length > 1 ) {
154153 courses_first_semester . push ( new Course ( $prev_course . getElementsByTagName ( "td" ) [ 0 ] . textContent . trim ( ) ,
155154 $prev_course . getElementsByTagName ( "td" ) [ 2 ] . getElementsByTagName ( "a" ) [ 0 ] . href ,
156155 $prev_course . getElementsByTagName ( "td" ) [ 1 ] . textContent . trim ( ) ,
@@ -163,11 +162,9 @@ function main_page () {
163162 }
164163 $ ( "table[border='0'][cellpadding='3'][cellspacing='1'][width='100%']" ) . prepend ( `<td style="background-color: white;" align="center"><button id="calculateCumulative">Calculate Cumulative GPA</button></td>` ) ;
165164 // passing courses in to possibly include current semester GPA if term has not finished yet.
166- if ( second_semester ) {
167- current_semester = 2 ;
168- }
165+
169166 $ ( "#calculateCumulative" ) . click ( function ( ) {
170- show_cumulative_gpa ( courses , current_term , current_semester ) ;
167+ show_cumulative_gpa ( courses , current_term , second_semester ) ;
171168 } ) ;
172169 saveGradesLocally ( student_name , courses ) ;
173170 // Hypo Grade Calculator
@@ -251,9 +248,9 @@ function calculate_cumulative_gpa (current_courses, current_term, current_semest
251248 continue ;
252249 } else {
253250 const $prev_course = element_list [ t ] ;
254- if ( $prev_course && $prev_course . getElementsByTagName ( "td" ) . length > 1 ) {
251+ if ( $prev_course ? .getElementsByTagName ( "td" ) . length > 1 ) {
255252 const course = new Course ( $prev_course . getElementsByTagName ( "td" ) [ 0 ] . textContent . trim ( ) , "" ,
256- $prev_course . getElementsByTagName ( "td" ) [ 1 ] . textContent . trim ( ) , 0 , "" ) ;
253+ $prev_course . getElementsByTagName ( "td" ) [ 1 ] . textContent . trim ( ) ) ;
257254 courses . push ( course ) ;
258255 }
259256 // Creates course object with each course from grade history page
@@ -271,15 +268,15 @@ function calculate_cumulative_gpa (current_courses, current_term, current_semest
271268 let include_current_semester = false ;
272269 if ( current_courses . length !== 0 ) {
273270 for ( let i = 0 ; i < current_courses . length ; i ++ ) {
274- if ( current_courses [ i ] . link . includes ( "begdate" ) ) {
271+ if ( new URL ( current_courses [ i ] . link ) . searchParams . get ( "begdate" ) ) {
275272 include_current_semester = true ;
276273 }
277274 }
278275 }
279276 // Handles edge case where grade history page is updated before semester end
280- if ( current_term_history === current_term && include_current_semester && current_term_grades . length === 2 && current_semester === 2 ) {
277+ if ( current_term_history === current_term && include_current_semester && current_term_grades . length === 2 && current_semester ) {
281278 include_current_semester = false ;
282- } else if ( current_term_history === current_term && include_current_semester && current_term_grades . length === 1 && current_semester === 1 ) {
279+ } else if ( current_term_history === current_term && include_current_semester && current_term_grades . length === 1 && current_semester === false ) {
283280 include_current_semester = false ;
284281 }
285282
0 commit comments