-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
249 lines (219 loc) · 6.52 KB
/
Copy pathutils.js
File metadata and controls
249 lines (219 loc) · 6.52 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
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
// Utility functions for the exam application
/**
* Shuffle array elements using Fisher-Yates algorithm
* @param {Array} array - Array to shuffle
* @returns {Array} - Shuffled array
*/
export function shuffleArray(array) {
const shuffled = [...array];
for (let i = shuffled.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
}
return shuffled;
}
/**
* Calculate exam score
* @param {Array} userAnswers - Array of user's selected answers
* @param {Array} questions - Array of questions with correct answers
* @returns {Object} - Score object with correct, total, percentage, and details
*/
export function calculateScore(userAnswers, questions) {
let correct = 0;
const details = [];
questions.forEach((question, index) => {
const userAnswer = userAnswers[index];
const isCorrect = userAnswer === question.correct;
if (isCorrect) {
correct++;
}
details.push({
questionId: question.id,
userAnswer: userAnswer,
correctAnswer: question.correct,
isCorrect: isCorrect,
explanation: question.explanation
});
});
const total = questions.length;
const percentage = Math.round((correct / total) * 100);
return {
correct,
total,
percentage,
details
};
}
/**
* Format time duration in seconds to readable format
* @param {number} seconds - Duration in seconds
* @returns {string} - Formatted time string
*/
export function formatTime(seconds) {
const hours = Math.floor(seconds / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
const secs = seconds % 60;
if (hours > 0) {
return `${hours}:${minutes.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
}
return `${minutes}:${secs.toString().padStart(2, '0')}`;
}
/**
* Start countdown timer
* @param {number} seconds - Total seconds for timer
* @param {Function} callback - Callback function called every second
* @returns {Object} - Timer object with stop method
*/
export function startTimer(seconds, callback) {
let remaining = seconds;
let intervalId = null;
const update = () => {
if (remaining <= 0) {
clearInterval(intervalId);
callback(0);
return;
}
callback(remaining);
remaining--;
};
// Call immediately
update();
// Then start interval
intervalId = setInterval(update, 1000);
return {
stop: () => clearInterval(intervalId)
};
}
/**
* Create progress percentage
* @param {number} current - Current progress
* @param {number} total - Total items
* @returns {number} - Percentage (0-100)
*/
export function getProgressPercentage(current, total) {
return Math.round((current / total) * 100);
}
/**
* Generate random exam session ID
* @returns {string} - Unique session ID
*/
export function generateSessionId() {
return 'CEH-' + Date.now() + '-' + Math.random().toString(36).substr(2, 9).toUpperCase();
}
/**
* Save exam results to localStorage
* @param {Object} results - Exam results object
*/
export function saveResults(results) {
try {
localStorage.setItem('ceh-exam-results', JSON.stringify(results));
} catch (error) {
console.error('Failed to save results to localStorage:', error);
}
}
/**
* Load exam results from localStorage
* @returns {Object|null} - Saved results or null
*/
export function loadResults() {
try {
const saved = localStorage.getItem('ceh-exam-results');
return saved ? JSON.parse(saved) : null;
} catch (error) {
console.error('Failed to load results from localStorage:', error);
return null;
}
}
/**
* Clear saved results from localStorage
*/
export function clearResults() {
try {
localStorage.removeItem('ceh-exam-results');
} catch (error) {
console.error('Failed to clear results from localStorage:', error);
}
}
/**
* Validate exam configuration
* @param {Object} config - Exam configuration
* @returns {Object} - Validation result
*/
export function validateConfig(config) {
const errors = [];
if (!config || typeof config !== 'object') {
errors.push('Configuration must be an object');
return { isValid: false, errors };
}
if (!config.questions || !Array.isArray(config.questions) || config.questions.length === 0) {
errors.push('Questions array is required and must not be empty');
}
if (config.timeLimit && (typeof config.timeLimit !== 'number' || config.timeLimit <= 0)) {
errors.push('Time limit must be a positive number');
}
if (config.passingScore && (typeof config.passingScore !== 'number' || config.passingScore < 0 || config.passingScore > 100)) {
errors.push('Passing score must be a number between 0 and 100');
}
return {
isValid: errors.length === 0,
errors
};
}
/**
* Debounce function to limit the rate of function calls
* @param {Function} func - Function to debounce
* @param {number} wait - Wait time in milliseconds
* @returns {Function} - Debounced function
*/
export function debounce(func, wait) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
/**
* Get grade based on percentage
* @param {number} percentage - Score percentage
* @returns {string} - Grade letter
*/
export function getGrade(percentage) {
if (percentage >= 90) return 'A+';
if (percentage >= 85) return 'A';
if (percentage >= 80) return 'A-';
if (percentage >= 75) return 'B+';
if (percentage >= 70) return 'B';
if (percentage >= 65) return 'B-';
if (percentage >= 60) return 'C+';
if (percentage >= 55) return 'C';
if (percentage >= 50) return 'C-';
if (percentage >= 45) return 'D+';
if (percentage >= 40) return 'D';
return 'F';
}
/**
* Get grade description
* @param {string} grade - Grade letter
* @returns {string} - Grade description
*/
export function getGradeDescription(grade) {
const descriptions = {
'A+': 'Excellent - Outstanding performance',
'A': 'Excellent - Very good performance',
'A-': 'Excellent - Good performance',
'B+': 'Good - Above average performance',
'B': 'Good - Average performance',
'B-': 'Good - Below average performance',
'C+': 'Fair - Above passing performance',
'C': 'Fair - Average passing performance',
'C-': 'Fair - Below average passing performance',
'D+': 'Poor - Just above failing',
'D': 'Poor - Failing performance',
'F': 'Fail - Below passing standards'
};
return descriptions[grade] || 'Unknown grade';
}