-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
89 lines (76 loc) · 2.88 KB
/
script.js
File metadata and controls
89 lines (76 loc) · 2.88 KB
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
let users = [];
const states = [
'Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado',
'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho',
'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana',
'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota',
'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada',
'New Hampshire', 'New Jersey', 'New Mexico', 'New York',
'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon',
'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota',
'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington',
'West Virginia', 'Wisconsin', 'Wyoming',
// Canadian Provinces and Territories
'Alberta', 'British Columbia', 'Manitoba', 'New Brunswick',
'Newfoundland and Labrador', 'Nova Scotia', 'Ontario', 'Prince Edward Island',
'Quebec', 'Saskatchewan', 'Northwest Territories', 'Nunavut', 'Yukon'
];
// Fill the dropdown on page load
const stateSelect = document.getElementById('stateSelect');
states.forEach(state => {
const option = document.createElement('option');
option.value = state;
option.textContent = state;
stateSelect.appendChild(option);
});
document.getElementById('fileInput').addEventListener('change', handleFileUpload);
function handleFileUpload(event) {
const file = event.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = function(e) {
try {
const rawData = JSON.parse(e.target.result);
users = Object.values(rawData);
console.log('Users loaded:', users);
} catch (err) {
alert('Invalid JSON file.');
}
};
reader.readAsText(file);
}
function findFriends() {
const selectedState = stateSelect.value;
if (!selectedState) {
alert('Please select your state.');
return;
}
if (users.length === 0) {
alert('No user data loaded.');
return;
}
const myState = selectedState.toLowerCase();
// Filter users by matching state
const matchedUsers = users.filter(user =>
user.state && user.state.toLowerCase() === myState
);
// Limit to 5
const topMatches = matchedUsers.slice(0, 5);
// Display
const resultsDiv = document.getElementById('friendResults');
resultsDiv.innerHTML = `<h2>Friends in ${selectedState}</h2>` +
(topMatches.map(user => `
<div class="friend-card">
<img class="cat-avatar" src="https://cataas.com/cat?width=250&height=250&${Math.random()}" alt="Cat Avatar" loading="lazy">
<div class="friend-info">
<p><strong>${user.name}</strong> (${user.github_login})</p>
<p>Title: ${user.title}</p>
<p>State: ${user.state}</p>
<p><a href="https://github.com/${user.github_login}" target="_blank">View GitHub Profile</a></p>
</div>
</div>
`).join(''));
if (topMatches.length === 0) {
resultsDiv.innerHTML = `<p>No friends found in ${selectedState}.</p>`;
}
}