Changed around line 1
+ document.addEventListener('DOMContentLoaded', () => {
+ const textDisplay = document.getElementById('textDisplay');
+ const userInput = document.getElementById('userInput');
+ const wpmDisplay = document.getElementById('wpm');
+ const accuracyDisplay = document.getElementById('accuracy');
+ const timerDisplay = document.getElementById('timer');
+
+ const sampleTexts = [
+ "The quick brown fox jumps over the lazy dog.",
+ "Pack my box with five dozen liquor jugs.",
+ "How vexingly quick daft zebras jump!",
+ "The five boxing wizards jump quickly."
+ ];
+
+ let startTime;
+ let timer;
+ let isTyping = false;
+
+ function initializeTest() {
+ textDisplay.textContent = sampleTexts[Math.floor(Math.random() * sampleTexts.length)];
+ userInput.value = '';
+ wpmDisplay.textContent = '0';
+ accuracyDisplay.textContent = '0%';
+ timerDisplay.textContent = '60s';
+ }
+
+ function startTimer() {
+ let timeLeft = 60;
+ timer = setInterval(() => {
+ timeLeft--;
+ timerDisplay.textContent = timeLeft + 's';
+ if (timeLeft === 0) endTest();
+ }, 1000);
+ }
+
+ function calculateWPM() {
+ const words = userInput.value.trim().split(/\s+/).length;
+ const minutes = (Date.now() - startTime) / 60000;
+ return Math.round(words / minutes);
+ }
+
+ function calculateAccuracy() {
+ const targetText = textDisplay.textContent;
+ const userText = userInput.value;
+ let correct = 0;
+
+ for (let i = 0; i < userText.length; i++) {
+ if (userText[i] === targetText[i]) correct++;
+ }
+
+ return Math.round((correct / targetText.length) * 100);
+ }
+
+ function endTest() {
+ clearInterval(timer);
+ userInput.disabled = true;
+ isTyping = false;
+
+ // Update stats
+ const finalWPM = calculateWPM();
+ const finalAccuracy = calculateAccuracy();
+
+ if (finalWPM > parseInt(document.getElementById('bestWpm').textContent)) {
+ document.getElementById('bestWpm').textContent = finalWPM + ' WPM';
+ }
+
+ const testsCompleted = parseInt(document.getElementById('testsCompleted').textContent) + 1;
+ document.getElementById('testsCompleted').textContent = testsCompleted;
+ }
+
+ userInput.addEventListener('input', () => {
+ if (!isTyping) {
+ isTyping = true;
+ startTime = Date.now();
+ startTimer();
+ }
+
+ wpmDisplay.textContent = calculateWPM();
+ accuracyDisplay.textContent = calculateAccuracy() + '%';
+ });
+
+ document.addEventListener('keydown', (e) => {
+ if (e.code === 'Enter' && !isTyping) {
+ initializeTest();
+ userInput.disabled = false;
+ userInput.focus();
+ }
+ });
+
+ initializeTest();
+ });