-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
317 lines (268 loc) · 11.3 KB
/
script.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
document.addEventListener('DOMContentLoaded', function() {
fetch('https://raw.githubusercontent.com/MarvinAmine/it-is-hot-over-there/master/dataArray.json')
.then(response => response.json())
.then(data => {
window.dataArray = data; // Store the data in a global variable
// Calculate scores for each city and assign ranks
calculateScoresAndRanks();
populateTable(data);
})
.catch(error => console.error('Error fetching data: ', error));
});
document.addEventListener('DOMContentLoaded', function() {
// Fetch data and initialize the table
fetchDataAndInitialize();
// Event listener for window resize
window.addEventListener('resize', handleResize);
handleResize(); // Call on initial load
});
async function fetchDataAndInitialize() {
try {
const response = await fetch('https://raw.githubusercontent.com/MarvinAmine/it-is-hot-over-there/master/dataArray.json');
const data = await response.json();
window.dataArray = data;
calculateScoresAndRanks();
populateTable(window.dataArray);
} catch (error) {
console.error('Error fetching data:', error);
}
}
function handleResize() {
const isMobileView = window.innerWidth <= 600;
if (isMobileView) {
createMobileSortingOptions();
} else {
removeMobileSortingOptions();
}
populateTable(window.dataArray);
}
function createMobileSortingOptions() {
// Create the dropdown for sorting if it doesn't exist
if (!document.getElementById('sortSelect')) {
const inputGroup = document.createElement('div');
inputGroup.className = 'input-group';
inputGroup.innerHTML = `
<select id="sortSelect">
<option value="rank">Rank</option>
<option value="city">City</option>
<option value="country">Country</option>
<option value="crimeIndex">Crime Index</option>
<option value="tempInDegrees">Temperature</option>
<option value="airbnbAveragePricePerNight">Airbnb Price</option>
<option value="distance">Distance</option>
</select>
<button onclick="sortTableMobile()">Sort</button>
`;
document.body.insertBefore(inputGroup, document.getElementById('dataTable'));
}
}
function removeMobileSortingOptions() {
// Remove the sorting dropdown if it exists
const sortSelect = document.getElementById('sortSelect');
if (sortSelect) {
sortSelect.parentNode.remove();
}
}
function sortTableMobile() {
const sortField = document.getElementById('sortSelect').value;
sortTable(sortField);
}
async function updateDistances() {
const userCity = document.getElementById('userCity').value;
const userCountry = document.getElementById('userCountry').value;
async function fetchCoordinates(city, country) {
const response = await fetch(`https://nominatim.openstreetmap.org/search?city=${encodeURIComponent(city)}&country=${encodeURIComponent(country)}&format=json`);
const data = await response.json();
if (data && data.length > 0) {
return { lat: parseFloat(data[0].lat), lon: parseFloat(data[0].lon) };
}
return null;
}
function calculateDistance(lat1, lon1, lat2, lon2) {
const R = 6371; // Radius of the Earth in km
const dLat = (lat2 - lat1) * Math.PI / 180;
const dLon = (lon2 - lon1) * Math.PI / 180;
const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) *
Math.sin(dLon / 2) * Math.sin(dLon / 2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return R * c; // Distance in km
}
try {
const userCoords = await fetchCoordinates(userCity, userCountry);
if (userCoords) {
window.dataArray.forEach(item => {
if (item.latitude && item.longitude) {
item.distance = calculateDistance(userCoords.lat, userCoords.lon, parseFloat(item.latitude), parseFloat(item.longitude));
} else {
item.distance = "Unknown";
}
});
}
populateTable(window.dataArray);
} catch (error) {
console.error('Error calculating distances:', error);
}
}
function calculateLogarithmicTicketPrice(distance) {
// Coefficients for the logarithmic model
const a = 0.05;
const b = 150;
// Calculate the price using the logarithmic formula
// Adding a small value to distance to avoid log(0) issue
const price = (a * distance + b) * 0.73;
return price > 0 ? price : 0; // Ensure the ticket price is not negative
}
function calculateScore(item) {
const weights = { crimeWeight: 1, airbnbWeight: 1, ticketPriceWeight: 1 }; // Adjust weights as needed
let score = 0;
// Adjust the crime score based on criteria
let crimeScore = item.crimeIndex ? parseFloat(item.crimeIndex) : 0;
if (crimeScore < 30) {
// Lower score for low crime rate
score -= crimeScore * weights.crimeWeight;
} else if (crimeScore > 55) {
// Higher score for high crime rate
score += crimeScore * weights.crimeWeight * 2;
} else {
// Neutral scoring for moderate crime rate
score += crimeScore * weights.crimeWeight;
}
// Score for Airbnb price - lower price gets lower score
let airbnbScore = item.airbnbAveragePricePerNight ? parseFloat(item.airbnbAveragePricePerNight) : 0;
score += airbnbScore * weights.airbnbWeight;
// Score for ticket price - lower price gets lower score
let ticketPrice = item.distance ? calculateLogarithmicTicketPrice(item.distance) : 0;
score += ticketPrice * weights.ticketPriceWeight;
return score;
}
function calculateScoresAndRanks() {
window.dataArray.forEach(item => {
item.score = calculateScore(item);
});
// Sort data based on score in ascending order (lower score is better)
window.dataArray.sort((a, b) => a.score - b.score);
// Assign ranks based on the sorted order
window.dataArray.forEach((item, index) => {
item.rank = index + 1;
console.log(`City: ${item.city}, Score: ${item.score}, Rank: ${item.rank}`); // Debugging line
});
}
let currentSort = {
field: '',
order: ''
};
function populateTable(data) {
const tableBody = document.getElementById('dataTable').getElementsByTagName('tbody')[0];
tableBody.innerHTML = '';
data.forEach((item) => {
let row = tableBody.insertRow();
// Rank
let cellRank = row.insertCell();
cellRank.setAttribute('data-label', 'Rank');
cellRank.className = 'col-rank';
cellRank.textContent = item.rank;
// City
let cellCity = row.insertCell();
cellCity.setAttribute('data-label', 'City');
cellCity.className = 'col-city';
cellCity.textContent = item.city;
// Country
let cellCountry = row.insertCell();
cellCountry.setAttribute('data-label', 'Country');
cellCountry.className = 'col-rank';
cellCountry.textContent = item.country;
// Crime Index
let cellCrimeIndex = row.insertCell();
cellCrimeIndex.setAttribute('data-label', 'Crime Index');
cellCrimeIndex.className = 'col-crime-index';
cellCrimeIndex.textContent = item.crimeIndex;
// Temperature
let cellTemp = row.insertCell();
cellTemp.setAttribute('data-label', 'Temperature (°C / °F)');
cellTemp.className = 'col-temperature';
cellTemp.textContent = item.tempInDegrees;
// Airbnb Price
let cellAirbnbPrice = row.insertCell();
cellAirbnbPrice.setAttribute('data-label', 'Airbnb Price (USD/Night)');
cellAirbnbPrice.className = 'col-airbnb';
cellAirbnbPrice.textContent = item.airbnbAveragePricePerNight;
// Country Index Used
let cellCountryIndexUsed = row.insertCell();
cellCountryIndexUsed.className = 'col-index-used';
cellCountryIndexUsed.setAttribute('data-label', 'Country Index Used');
cellCountryIndexUsed.textContent = item.isCountryCrimeIndex ? 'Yes' : 'No';
// Distance
let cellDistance = row.insertCell();
cellDistance.className = 'col-distance';
cellDistance.setAttribute('data-label', 'Distance (km)');
cellDistance.textContent = item.distance ? item.distance.toFixed(2) + ' km' : 'Not calculated';
// Estimated Airplane Ticket Price
let cellTicketPrice = row.insertCell();
cellTicketPrice.className = 'col-ticket-price';
cellTicketPrice.setAttribute('data-label', 'Estimated Airplane Ticket Price');
let ticketPrice = "Not calculated";
if (item.distance && !isNaN(item.distance)) {
ticketPrice = calculateLogarithmicTicketPrice(item.distance).toFixed(2) + ' USD';
}
cellTicketPrice.textContent = ticketPrice;
});
}
document.querySelectorAll('#dataTable th').forEach(header => {
header.addEventListener('click', () => {
sortTable(header.getAttribute('id'));
});
});
function sortTable(sortField) {
if (currentSort.field === sortField) {
currentSort.order = currentSort.order === 'asc' ? 'desc' : 'asc';
} else {
currentSort.field = sortField;
currentSort.order = 'asc';
}
const sortedData = dataArray.sort((a, b) => {
let valA = a[sortField];
let valB = b[sortField];
if (sortField === 'crimeIndex' || sortField === 'airbnbAveragePricePerNight') {
valA = valA === "" ? (currentSort.order === 'asc' ? Infinity : -Infinity) : parseFloat(valA);
valB = valB === "" ? (currentSort.order === 'asc' ? Infinity : -Infinity) : parseFloat(valB);
}
if (sortField === 'isCountryCrimeIndex') {
valA = valA === true ? 1 : 0;
valB = valB === true ? 1 : 0;
}
if (valA < valB) {
return -1;
}
if (valA > valB) {
return 1;
}
return 0;
});
if (currentSort.order === 'desc') {
sortedData.reverse();
}
populateTable(sortedData);
}
function searchTable() {
let input, filter, table, tr, txtValue;
input = document.getElementById('searchInput');
filter = input.value.toUpperCase();
table = document.getElementById('dataTable');
tr = table.getElementsByTagName('tr');
// Loop through all table rows, and hide those who don't match the search query
for (let i = 1; i < tr.length; i++) { // Start from 1 to skip the header row
let tds = tr[i].getElementsByTagName('td');
let found = false;
for (let j = 0; j < tds.length; j++) {
if (tds[j]) {
txtValue = tds[j].textContent || tds[j].innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
found = true;
break;
}
}
}
tr[i].style.display = found ? "" : "none";
}
}