From 0e9a1a8018d528304365359cf668d892231850d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20M=C3=BCller?= Date: Wed, 13 Oct 2021 16:10:12 +0200 Subject: [PATCH] #11: Adding method to calculate semester --- scripts.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/scripts.js b/scripts.js index 6b275cf..a214553 100644 --- a/scripts.js +++ b/scripts.js @@ -83,6 +83,29 @@ let courseLinks = { 'TINF20B5': {user: 'strand', file: 'TINF20B5'} } +/** + * Calculates, in which semester the given course currently is + * @param course The course to calculate the semester for + * @returns {number} The number of the semester they are in + */ +function calculateSemester(course) { + let yearRegex = new RegExp('[a-zA-Z]*([0-9]{2})B[0-9]'); + + let match = yearRegex.exec(course); + let startYear = '20'; + if (match !== null) { + startYear += match[1]; + } + + let currentYear = new Date().getFullYear(); + + let yearDifference = currentYear - startYear; + + let semesters = yearDifference * 2 + 1; + + return semesters; +} + /* Toggle Dark-Mode */ const toggleDarkMode = () => {