-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
90 lines (79 loc) · 2.82 KB
/
Copy pathscript.js
File metadata and controls
90 lines (79 loc) · 2.82 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
const textDisplay = document.getElementById("text-display");
const textInput = document.getElementById("text-input");
const timerElement = document.getElementById("timer");
const wpmElement = document.getElementById("wpm");
const accuracyElement = document.getElementById("accuracy");
const restartButton = document.getElementById("restart");
const sentences = [
"The quick brown fox jumps over the lazy dog.",
"JavaScript makes the web interactive and dynamic.",
"Type fast to test your speed and accuracy.",
"Programming is fun when you practice regularly.",
"Never stop learning because life never stops teaching."
];
let currentSentence = "";
let timer = 60;
let timerInterval;
let totalTyped = 0;
let correctTyped = 0;
let isTypingStarted = false;
// Load a new sentence
function loadSentence() {
currentSentence = sentences[Math.floor(Math.random() * sentences.length)];
textDisplay.innerHTML = currentSentence.split("").map(char => `<span>${char}</span>`).join("");
textInput.value = "";
textInput.disabled = false;
isTypingStarted = false;
clearInterval(timerInterval);
timerElement.textContent = "60";
wpmElement.textContent = "0";
accuracyElement.textContent = "0";
}
// Start the countdown timer
function startTimer() {
timer = 60;
timerElement.textContent = timer;
timerInterval = setInterval(() => {
timer--;
timerElement.textContent = timer;
if (timer <= 0) {
clearInterval(timerInterval);
textInput.disabled = true;
}
calculateWPM();
}, 1000);
}
// Calculate WPM and accuracy
function calculateWPM() {
let elapsedTime = (60 - timer) / 60;
let wordsTyped = textInput.value.trim().split(/\s+/).length;
let wpm = Math.floor(wordsTyped / (elapsedTime || 1));
wpmElement.textContent = isNaN(wpm) ? "0" : wpm;
let correctCharacters = 0;
let typedText = textInput.value;
let allSpans = document.querySelectorAll("#text-display span");
for (let i = 0; i < allSpans.length; i++) {
if (typedText[i] === currentSentence[i]) {
allSpans[i].style.color = "#00ff00"; // Green for correct
correctCharacters++;
} else {
allSpans[i].style.color = "#ff4b5c"; // Red for incorrect
}
}
totalTyped = typedText.length;
correctTyped = correctCharacters;
let accuracy = Math.floor((correctTyped / (totalTyped || 1)) * 100);
accuracyElement.textContent = accuracy;
}
// Event listeners
textInput.addEventListener("input", () => {
if (!isTypingStarted) {
isTypingStarted = true;
startTimer();
}
calculateWPM();
});
// Restart button
restartButton.addEventListener("click", loadSentence);
// Initialize
loadSentence();