-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
55 lines (49 loc) · 1.67 KB
/
index.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
const inputField = document.getElementById('inputField');
const timeDisplay = document.getElementById('time');
const correctWordsDisplay = document.getElementById('correctWords');
const wpmDisplay = document.getElementById('wpm');
const resetBtn = document.getElementById('resetBtn');
const quote = "The quick brown fox jumps over the lazy dog.";
let timer;
let startTime;
let correctWordsCount = 0;
// Initialize the test
function init() {
inputField.value = '';
correctWordsCount = 0;
timeDisplay.textContent = '0';
correctWordsDisplay.textContent = '0';
wpmDisplay.textContent = '0';
clearInterval(timer);
}
// Calculate words per minute
function calculateWPM() {
const elapsedTimeInSeconds = (Date.now() - startTime) / 1000;
const wordsTyped = inputField.value.trim().split(' ').length;
const wpm = Math.round((wordsTyped / elapsedTimeInSeconds) * 60);
return wpm;
}
// Update stats
function updateStats() {
const words = inputField.value.trim().split(' ');
correctWordsCount = words.filter(word => quote.includes(word)).length;
correctWordsDisplay.textContent = correctWordsCount;
wpmDisplay.textContent = calculateWPM();
}
// Start the test
function startTest() {
startTime = Date.now();
timer = setInterval(() => {
const elapsedTimeInSeconds = Math.floor((Date.now() - startTime) / 1000);
timeDisplay.textContent = elapsedTimeInSeconds;
updateStats();
}, 1000);
}
// Event listeners
resetBtn.addEventListener('click', init);
inputField.addEventListener('input', () => {
if (!timer) startTest();
updateStats();
});
// Initialize the test
init();