-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.js
256 lines (227 loc) · 9.08 KB
/
code.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/**
* Simple Code.org JS app to find the closest Wi-Fi hotspot in NYC
* Uses Code.org "NYC Public Wifi Locations" dataset
* Uses about 9,000,000 traversals to sort the list of over 2,500 distances
* @author John Brereton
* @since 4/20/2021
*/
// Create lists to represent the dataset
var providerList = getColumn("NYC Public Wifi Locations", "Provider");
var locationList = getColumn("NYC Public Wifi Locations", "Location");
var latitudeList = getColumn("NYC Public Wifi Locations", "Latitude");
var longitudeList = getColumn("NYC Public Wifi Locations", "Longitude");
var accessList = getColumn("NYC Public Wifi Locations", "Access Location");
var boroughList = getColumn("NYC Public Wifi Locations", "Borough");
var neighborhoodList = getColumn("NYC Public Wifi Locations", "Neighborhood");
var zipList = getColumn("NYC Public Wifi Locations", "Postal Code");
// List to store the distance between each access point location and the user entered location
var distList = [];
var sortedIdList = [];
// Miles are now used for all distance calculations
// var distListMiles = [];
// Booleans that indicate wheather or not an error has been made when entering latitude and longitude
var latError;
var longError;
// Stores the results page currently being viewed
var resultsPage = 0;
// Stores the reference number of the location being viewed on the info page
var infoId;
// Stores the distance between the user entered lat and long and the item being traversed
var dist;
// Miles are now used for all distance calculations
// var distMiles;
// Finds and organizes the closest hotspots when the go button is clicked
onEvent("goButton", "click", function() {
if(validateFields()) {
setScreen("loadingScreen");
generateDistList();
sortedIdList = sortList(distList)[1];
updateResults();
setScreen("resultsScreen");
}
});
// Returns to the home screen when the results screen back button is pressed
onEvent("resultsBackButton", "click", function() {
setScreen("homeScreen");
resultsPage = 0;
});
// Returns to the results screen when the info screen back button is pressed
onEvent("infoBackButton", "click", function() {
setScreen("resultsScreen");
});
// Changes to the previous page when the previous arrow is clicked
onEvent("prevButton", "click", function() {
if(resultsPage>0) {
resultsPage--;
updateResults();
}
});
// Changes to the next page when the next arrow is clicked
onEvent("nextButton", "click", function() {
if(resultsPage<512) {
resultsPage++;
updateResults();
}
});
// On event for the first result
// Sets the info id and runs the function to update the info screen
onEvent("result0", "click", function() {
infoId = (5*resultsPage);
updateInfo();
setScreen("infoScreen");
});
// On event for the second result
onEvent("result1", "click", function() {
infoId = (5*resultsPage)+1;
updateInfo();
setScreen("infoScreen");
});
// On event for the third result
onEvent("result2", "click", function() {
infoId = (5*resultsPage)+2;
updateInfo();
setScreen("infoScreen");
});
// On event for the fourth result
onEvent("result3", "click", function() {
infoId = (5*resultsPage)+3;
updateInfo();
setScreen("infoScreen");
});
// On event for the fifth result
onEvent("result4", "click", function() {
infoId = (5*resultsPage)+4;
updateInfo();
setScreen("infoScreen");
});
// Validates that the latitude and longitude values the user enters are valid
// Checks that they are not blank
// Checks that they are a number
// Checks that they are within the latitude and longitude of NYC
function validateFields() {
// Verifies latitude input
if(getText("latitudeInput") == "") {
setProperty("latitudeWarning", "text", "This is a required field");
latError = true;
}
else if(isNaN(getText("latitudeInput"))) {
setProperty("latitudeWarning", "text", "Please enter a valid number");
latError = true;
}
else if(getText("latitudeInput") > calcMaxMin(latitudeList)[0] || getText("latitudeInput") < calcMaxMin(latitudeList)[1]) {
setProperty("latitudeWarning", "text", "Please enter a latitudinal value within NYC");
latError = true;
}
else {
latError = false;
setProperty("latitudeWarning", "text", "");
}
// Verifies longitude input
if(getText("longitudeInput") == "") {
setProperty("longitudeWarning", "text", "This is a required field");
longError = true;
}
else if(isNaN(getText("longitudeInput"))) {
setProperty("longitudeWarning", "text", "Please enter a valid number");
longError = true;
}
else if(getText("longitudeInput") > calcMaxMin(longitudeList)[0] || getText("longitudeInput") < calcMaxMin(longitudeList)[1]) {
setProperty("longitudeWarning", "text", "Please enter a longitudinal value within NYC");
longError = true;
}
else {
longError = false;
setProperty("longitudeWarning", "text", "");
}
// Returns weather or not the user made a mistake in entering their latitude and longitude
if(latError == false && longError == false) {
return true;
}
else {
return false;
}
}
// Calculates the maximum and minimum value of an list
// Returns values as an list
function calcMaxMin(calcList) {
var max = calcList[0];
var min = calcList[0];
for(var i=0; i < calcList.length; i++) {
if(calcList[i] > max) {
max = calcList[i];
}
else if(calcList[i] < min) {
min = calcList[i];
}
}
return [max, min];
}
// Generates list of distances from the user in miles and coordinate values
function generateDistList() {
var userLatitude = getText("latitudeInput");
var userLongitude = getText("longitudeInput");
for(var i = 0; i < providerList.length; i++) {
// Old method, used latitude and longitude wihtout miles conversion
// Since latitude and longitude are not equal measurements this method was not accurate
// dist = Math.sqrt(Math.abs((Math.abs(userLatitude)-Math.abs(latitudeList[i])))+Math.abs((Math.abs(userLongitude)-Math.abs(longitudeList[i]))));
// distList[i] = dist;
// New method, converts latitude and longitude to miles
dist = Math.round(Math.sqrt((Math.pow(Math.abs((userLatitude*69)-(latitudeList[i]*69)), 2)) + (Math.pow(Math.abs((userLongitude*54.6)-(longitudeList[i]*54.6)), 2)))*100)/100;
distList[i] = dist;
}
}
// Sorts list from least to greatest
// Returns list of sorted values and a list of their origional list ids
// Uses two for loops to go through every possible combination of two values in the list and see which one is greater
// If the larger number is in front of the smaller number their positions will be exchanged
function sortList(list) {
var sortedList = [];
var sortedIds = [];
var temp = 0;
sortedList = list;
sortedIds = createNumericalList(0, list.length);
for (var i = 0; i < sortedList.length; i++) {
for (var j=i; j < sortedList.length; j++) {
if (sortedList[j] < sortedList[i]) {
temp = sortedIds[j];
sortedIds[j] = sortedIds[i];
sortedIds[i] = temp;
temp = sortedList[j];
sortedList [j] = sortedList[i];
sortedList[i] = temp;
}
}
}
return [sortedList, sortedIds];
}
// Creates a list of consecutive integers
// Requires a starting(from) and ending(to) number
function createNumericalList(from, to) {
var list = [];
for (var i=from; i<=to; i++) {
appendItem(list, i);
}
return list;
}
// Updates the results page
// Shows results in order from closest to furthest using the sorted id list
function updateResults() {
for (var i=0; i<=4; i++) {
setProperty("result" + i, "text", "Location: " + locationList[sortedIdList[(5*resultsPage)+i]] + " " + boroughList[sortedIdList[(5*resultsPage)+i]] +
" NYC\nProvider: " + providerList[sortedIdList[(5*resultsPage)+i]]);
}
setProperty("pageLabel", "text", "Page: " + (resultsPage+1) + " of 513");
}
// Updates the info page
// Info id represents which button was clicked
// Sorted id list is used to make info corespond to correct button on results page
// Only takes the first 20 values of the string to prevent the string from expanding into a second line
function updateInfo() {
setProperty("infoOutput", "text", distList[sortedIdList[infoId]] + " Miles" +
"\n" + providerList[sortedIdList[infoId]] +
"\n" + locationList[sortedIdList[infoId]].toString().substring(0, 20) +
"\n" + accessList[sortedIdList[infoId]].toString().substring(0, 20) +
"\n" + boroughList[sortedIdList[infoId]].toString().substring(0, 20) +
"\n" + neighborhoodList[sortedIdList[infoId]].toString().substring(0, 20) +
"\n" + zipList[sortedIdList[infoId]].toString().substring(0, 20));
}