-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
241 lines (208 loc) · 9.03 KB
/
script.js
File metadata and controls
241 lines (208 loc) · 9.03 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
class TypingTest {
constructor() {
this.sampleTexts = [
"The quick brown fox jumps over the lazy dog. Programming is the process of creating a set of instructions that tell a computer how to perform a task.",
"Software development is the process of conceiving, specifying, designing, programming, documenting, testing, and bug fixing involved in creating and maintaining applications.",
"Clean code is code that is easy to understand and easy to change. It reads like well-written prose and makes the programmer's intent clear to other developers.",
"TypeScript is a strongly typed programming language that builds on JavaScript giving you better tooling at any scale. It helps catch errors early.",
"React is a JavaScript library for building user interfaces. It lets you compose complex UIs from small and isolated pieces of code called components."
];
this.currentText = '';
this.words = [];
this.currentWordIndex = 0;
this.startTime = null;
this.timer = null;
this.timeLimit = 60;
this.timeLeft = this.timeLimit;
this.correctChars = 0;
this.totalChars = 0;
this.errors = 0;
this.isRunning = false;
this.initializeElements();
this.bindEvents();
this.loadNewText();
}
initializeElements() {
this.textDisplay = document.getElementById('text-display');
this.typingInput = document.getElementById('typing-input');
this.restartBtn = document.getElementById('restart-btn');
this.wpmDisplay = document.getElementById('wpm');
this.accuracyDisplay = document.getElementById('accuracy');
this.timerDisplay = document.getElementById('timer');
this.resultsPanel = document.getElementById('results-panel');
this.finalWpm = document.getElementById('final-wpm');
this.finalAccuracy = document.getElementById('final-accuracy');
this.finalTime = document.getElementById('final-time');
this.finalErrors = document.getElementById('final-errors');
this.newTestBtn = document.getElementById('new-test-btn');
// Select progress ring paths by ID (add these IDs in your HTML)
this.wpmProgress = document.getElementById('wpm-progress');
this.accuracyProgress = document.getElementById('accuracy-progress');
}
bindEvents() {
this.typingInput.addEventListener('input', (e) => this.handleInput(e));
this.restartBtn.addEventListener('click', () => this.restartTest());
this.newTestBtn.addEventListener('click', () => this.restartTest());
// Removed space-preventing listener to allow spaces in input
}
loadNewText() {
const randomIndex = Math.floor(Math.random() * this.sampleTexts.length);
this.currentText = this.sampleTexts[randomIndex];
this.words = this.currentText.split(' ');
this.currentWordIndex = 0;
this.renderText();
}
renderText() {
let html = '';
this.words.forEach((word, index) => {
let wordClass = '';
if (index < this.currentWordIndex) {
wordClass = 'correct';
} else if (index === this.currentWordIndex) {
wordClass = 'current-word';
}
html += `<span class="${wordClass}">${word}</span> `;
});
this.textDisplay.innerHTML = html;
}
renderTextWithInput(input) {
let html = '';
this.words.forEach((word, index) => {
if (index < this.currentWordIndex) {
html += `<span class="correct">${word}</span> `;
} else if (index === this.currentWordIndex) {
// Show per-character correctness
let wordHtml = '';
for (let i = 0; i < word.length; i++) {
let charClass = '';
if (i < input.length) {
charClass = (input[i] === word[i]) ? 'correct' : 'incorrect';
}
wordHtml += `<span class="${charClass}">${word[i]}</span>`;
}
// Extra chars typed beyond word length are incorrect
if (input.length > word.length) {
for (let i = word.length; i < input.length; i++) {
wordHtml += `<span class="incorrect">${input[i]}</span>`;
}
}
html += `<span class="current-word">${wordHtml}</span> `;
} else {
html += `<span>${word}</span> `;
}
});
this.textDisplay.innerHTML = html;
}
startTest() {
if (!this.isRunning) {
this.isRunning = true;
this.startTime = Date.now();
this.startTimer();
this.typingInput.focus();
}
}
startTimer() {
this.timer = setInterval(() => {
this.timeLeft--;
this.timerDisplay.textContent = `${this.timeLeft}s`;
this.updateProgressRings();
if (this.timeLeft <= 0) {
this.endTest();
}
}, 1000);
}
updateProgressRings() {
if (this.wpmProgress) {
const wpmValue = parseInt(this.wpmDisplay.textContent) || 0;
// Stroke dashoffset: 100% - (wpm capped at 100)
const wpmOffset = 100 - Math.min(wpmValue, 100);
this.wpmProgress.style.strokeDashoffset = wpmOffset;
}
if (this.accuracyProgress) {
const accuracyText = this.accuracyDisplay.textContent;
const accuracyValue = parseInt(accuracyText) || 100;
const accuracyOffset = 100 - accuracyValue;
this.accuracyProgress.style.strokeDashoffset = accuracyOffset;
}
}
handleInput(e) {
if (!this.isRunning) {
this.startTest();
}
const input = this.typingInput.value;
const currentWord = this.words[this.currentWordIndex];
if (input.endsWith(' ')) {
const typedWord = input.trim();
if (typedWord === currentWord) {
this.correctChars += currentWord.length;
} else {
const errorsInWord = this.countErrors(currentWord, typedWord);
this.errors += errorsInWord;
// Add correct chars only for correctly typed chars
this.correctChars += Math.max(currentWord.length - errorsInWord, 0);
}
this.totalChars += currentWord.length;
this.currentWordIndex++;
if (this.currentWordIndex >= this.words.length) {
this.loadNewText();
}
this.typingInput.value = '';
this.renderText();
this.updateStats();
} else {
// Update current word highlighting with per-character correctness
this.renderTextWithInput(input);
}
}
countErrors(word1, word2) {
let errors = 0;
const len = Math.max(word1.length, word2.length);
for (let i = 0; i < len; i++) {
if (word1[i] !== word2[i]) {
errors++;
}
}
return errors;
}
updateStats() {
const elapsedTime = (Date.now() - this.startTime) / 1000 / 60; // minutes
const wpm = elapsedTime > 0 ? Math.round((this.correctChars / 5) / elapsedTime) : 0;
const accuracy = this.totalChars > 0
? Math.round((this.correctChars / this.totalChars) * 100)
: 100;
this.wpmDisplay.textContent = isNaN(wpm) ? '0' : wpm;
this.accuracyDisplay.textContent = `${accuracy}%`;
this.updateProgressRings();
}
endTest() {
clearInterval(this.timer);
this.isRunning = false;
this.finalWpm.textContent = this.wpmDisplay.textContent;
this.finalAccuracy.textContent = this.accuracyDisplay.textContent;
this.finalTime.textContent = `${this.timeLimit - this.timeLeft}s`;
this.finalErrors.textContent = this.errors;
this.resultsPanel.classList.remove('hidden');
this.typingInput.disabled = true;
}
restartTest() {
clearInterval(this.timer);
this.isRunning = false;
this.currentWordIndex = 0;
this.timeLeft = this.timeLimit;
this.correctChars = 0;
this.totalChars = 0;
this.errors = 0;
this.wpmDisplay.textContent = '0';
this.accuracyDisplay.textContent = '100%';
this.timerDisplay.textContent = `${this.timeLimit}s`;
this.typingInput.value = '';
this.typingInput.disabled = false;
this.resultsPanel.classList.add('hidden');
this.loadNewText();
this.updateProgressRings();
}
}
// Initialize the typing test when the page loads
document.addEventListener('DOMContentLoaded', () => {
new TypingTest();
});