This repository was archived by the owner on Nov 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmajor-finder.js
172 lines (140 loc) · 5.88 KB
/
major-finder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
Major Finder 2.0.0
Find majors you are eligible for at UofT
Copyright (c) 2016 - 2017 Patrick Harris (https://plh.io)
Licensed under the MIT License
https://raw.githubusercontent.com/patrickleweryharris/major-finder/master/LICENSE
Project site: https://plh.io/major-finder
Github site: http://github.com/patrickleweryharris/major-finder
*/
/*
Getting campus selection
*/
var campusSelection;
function getSelection() {
campusSelection = $('input[name=group1]:checked').val();
}
/*
* Getting user input and sanitizing it
*/
var userInput;
function getInput() {
var userInputText = document.getElementById('course_input').value;
userInput = userInputText.toUpperCase().split(',');
//Loop over and sanitize course inputs
for (i = 0; i < userInput.length; i++) {
userInput[i] = userInput[i].trim();
userInput[i] = userInput[i].slice(0, 6); // Take only first six characters (i.e. "MAT135h" becomes "MAT135")
}
}
/*
* Finding majors
*/
var jsonStr;
var seenPrograms = [];
function findMajors() {
getSelection();
if (campusSelection == 'UTSG') {
jsonStr = 'https://raw.githubusercontent.com/patrickleweryharris/major-finder/master/json/majors.json';
$('#georgi').hide();
$('#missy').hide();
$('#siberia').hide();
}
if (campusSelection == 'UTM') {
jsonStr = 'https://raw.githubusercontent.com/patrickleweryharris/major-finder/master/json/utm.json';
$('#missy').show();
$('#georgi').hide();
$('#siberia').hide();
}
if (campusSelection == 'UTSC') {
jsonStr = 'https://raw.githubusercontent.com/patrickleweryharris/major-finder/master/json/utsc.json';
$('#siberia').show();
$('#georgi').hide();
$('#missy').hide();
}
$.getJSON(jsonStr, function(data) {
getInput();
var type1Output = "";
var type2Output = "";
var type2LOutput = "";
var type3Output = "";
var autoTypeOne = "Additionally, you are eligble for all Type One programs not already listed <br> You can view them <a href='https://plh.io/major-finder/type1'>here</a>";
if (userInput != '') {
type1Output = "You are eligible for: <br> Type 1 Programs: <ul>";
type2Output = "Type 2 Programs: <ul>";
type2LOutput = "Type 2L Programs: <ul>";
type3Output = "Type 3 Progrms: <ul>";
for (i = 0; i < data.length; i++) {
//Check if the input meets any post requirements
var flag = isSub(userInput, data[i].requirements);
if (flag === true) {
if ($.inArray(data[i].postName, seenPrograms) == -1){
seenPrograms.push(data[i].postName);
if (data[i].type == "1"){
type1Output = type1Output.concat("<li>");
type1Output = type1Output.concat("<a href=");
type1Output = type1Output.concat(data[i].calLink);
type1Output = type1Output.concat(">");
type1Output = type1Output.concat(data[i].postName);
type1Output = type1Output.concat("</a></li>");
}
else if (data[i].type == "2"){
type2Output = type2Output.concat("<li>");
type2Output = type2Output.concat("<a href=");
type2Output = type2Output.concat(data[i].calLink);
type2Output = type2Output.concat(">");
type2Output = type2Output.concat(data[i].postName);
type2Output = type2Output.concat("</a></li>");
}
else if (data[i].type == "2L"){
type2LOutput = type2LOutput.concat("<li>");
type2LOutput = type2LOutput.concat("<a href=");
type2LOutput = type2LOutput.concat(data[i].calLink);
type2LOutput = type2LOutput.concat(">");
type2LOutput = type2LOutput.concat(data[i].postName);
type2LOutput = type2LOutput.concat("</a></li>");
}
else if (data[i].type == "3"){
type3Output = type3Output.concat("<li>");
type3Output = type3Output.concat("<a href=");
type3Output = type3Output.concat(data[i].calLink);
type3Output = type3Output.concat(">");
type3Output = type3Output.concat(data[i].postName);
type3Output = type3Output.concat("</a></li>");
}
}
}
}
type1Output = type1Output.concat("</ul>");
type2Output = type2Output.concat("</ul>");
type2LOutput = type2LOutput.concat("</ul>");
type3Output = type3Output.concat("</ul>");
document.getElementById("eligible_programs").innerHTML = type1Output + "<br>" + type2Output + "<br>" + type2LOutput + "<br>" + type3Output + "<br>" + autoTypeOne;
} else {
type1Output = "You did not enter any courses";
document.getElementById("eligible_programs").innerHTML = type1Output;
}
});
}
/*
Helper function for comparing the arrays
Sort both arrays, traverse both.
Compare the elements. If an element in the post_reqs is not found in the
inputted courses, then the courses do not meet the post requirements
*/
function isSub(courses, post_reqs) {
courses.sort();
post_reqs.sort();
var i, j;
for (i = 0, j = 0; i < courses.length && j < post_reqs.length;) {
if (courses[i] < post_reqs[j]) {
++i; // Something appears in input that reqs doesn't need or have
} else if (courses[i] == post_reqs[j]) {
++i;
++j;
} else {
return false;
}
}
return j == post_reqs.length;
}