-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
160 lines (138 loc) · 5.05 KB
/
Copy pathscript.js
File metadata and controls
160 lines (138 loc) · 5.05 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
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
// Global variable to store country data
let countryData = [];
// Fetch data from data.json when the page loads
document.addEventListener('DOMContentLoaded', () => {
fetch('data.json')
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
countryData = data;
renderTable(data);
})
.catch(error => {
console.error('Error fetching data:', error);
const tbody = document.querySelector('#requirements-table tbody');
tbody.innerHTML = '<tr><td colspan="5" style="text-align:center; color:red;">无法加载数据,请确保在本地服务器环境下运行 (例如使用 Live Server 或 python -m http.server)。</td></tr>';
});
// Setup event listener for the test button
const testBtn = document.getElementById('test-btn');
testBtn.addEventListener('click', handleTest);
});
// Render the requirements table
function renderTable(data) {
const tbody = document.querySelector('#requirements-table tbody');
tbody.innerHTML = ''; // Clear existing rows
data.forEach(item => {
const tr = document.createElement('tr');
// Country
const tdCountry = document.createElement('td');
tdCountry.textContent = item.country;
tdCountry.style.fontWeight = 'bold';
tr.appendChild(tdCountry);
// IELTS
const tdIelts = document.createElement('td');
tdIelts.textContent = item.ielts;
tr.appendChild(tdIelts);
// TOEFL
const tdToefl = document.createElement('td');
tdToefl.textContent = item.toefl;
tr.appendChild(tdToefl);
// PTE
const tdPte = document.createElement('td');
tdPte.textContent = item.pte;
tr.appendChild(tdPte);
// Source
const tdSource = document.createElement('td');
const aSource = document.createElement('a');
aSource.href = item.source_url;
aSource.textContent = item.source_name;
aSource.target = '_blank'; // Open in new tab
tdSource.appendChild(aSource);
tr.appendChild(tdSource);
tbody.appendChild(tr);
});
}
// Handle the matching test logic
function handleTest() {
const examType = document.getElementById('exam-type').value;
const scoreInput = document.getElementById('score-input').value;
const resultsContainer = document.getElementById('results-container');
const resultsGrid = document.getElementById('results-grid');
// Validation
if (!scoreInput || isNaN(scoreInput)) {
alert('请输入有效的分数!');
return;
}
const userScore = parseFloat(scoreInput);
resultsGrid.innerHTML = ''; // Clear previous results
// Calculate status for each country
countryData.forEach(country => {
const reqScore = country[examType];
let status = ''; // 'safe', 'marginal', 'difficult'
let statusText = '';
let statusIcon = '';
let badgeClass = '';
// Algorithm for matching degree
if (examType === 'ielts') {
if (userScore >= reqScore + 0.5) {
status = 'safe';
} else if (userScore >= reqScore) {
status = 'marginal';
} else {
status = 'difficult';
}
} else {
// TOEFL and PTE logic
if (userScore >= reqScore + 5) {
status = 'safe';
} else if (userScore >= reqScore) {
status = 'marginal';
} else {
status = 'difficult';
}
}
// Set UI text and classes based on status
if (status === 'safe') {
statusText = '稳了';
statusIcon = '🟢';
badgeClass = 'bg-safe';
} else if (status === 'marginal') {
statusText = '勉强';
statusIcon = '🟡';
badgeClass = 'bg-marginal';
} else {
statusText = '困难';
statusIcon = '🔴';
badgeClass = 'bg-difficult';
}
// Create result card
const card = document.createElement('div');
card.className = `result-card status-${status}`;
const examNameMap = {
'ielts': '雅思',
'toefl': '托福',
'pte': 'PTE'
};
card.innerHTML = `
<div class="country-name">
${country.country}
<span>${statusIcon}</span>
</div>
<div class="req-score">
要求${examNameMap[examType]}:${reqScore} 分
</div>
<div>
<span class="status-badge ${badgeClass}">${statusText}</span>
</div>
`;
resultsGrid.appendChild(card);
});
// Show the results container
resultsContainer.classList.remove('hidden');
// Smooth scroll to results
resultsContainer.scrollIntoView({ behavior: 'smooth', block: 'start' });
}