-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript2.js
More file actions
207 lines (173 loc) Β· 7.34 KB
/
script2.js
File metadata and controls
207 lines (173 loc) Β· 7.34 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
const quoteDisplay = document.getElementById('quoteDisplay');
const quoteInput = document.getElementById('quoteInput');
const timeElement = document.getElementById('time');
const wpmElement = document.getElementById('wpm');
const accuracyElement = document.getElementById('accuracy');
const startBtn = document.getElementById('startBtn');
const newQuoteBtn = document.getElementById('newQuoteBtn');
const progressBar = document.getElementById('progressBar');
const resultModal = document.getElementById('resultModal');
const resultWpm = document.getElementById('wpm-value');
const resultAccuracy = document.getElementById('accuracy-value');
const resultTime = document.getElementById('time-value');
const resultProgressBar = document.getElementById('resultProgressBar');
const feedbackMessage = document.getElementById('feedback-message');
const closeModalBtn = document.getElementById('closeModalBtn');
let time = 0;
let timer;
let isPlaying = false;
let wpm = 0;
let accuracy = 0;
let correctChars = 0;
let totalChars = 0;
let currentQuote = '';
let currentCharIndex = 0;
const quotes = [
"The quick brown fox jumps over the lazy dog.",
"Programming is the art of telling another human what one wants the computer to do.",
"The only way to learn a new programming language is by writing programs in it.",
"The best error message is the one that never shows up.",
"First, solve the problem. Then, write the code.",
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand.",
"The most disastrous thing that you can ever learn is your first programming language.",
"The most important property of a program is whether it accomplishes the intention of its user.",
"The function of good software is to make the complex appear to be simple.",
"Simplicity is the soul of efficiency.",
"Code is like humor. When you have to explain it, it's bad.",
"Programming isn't about what you know; it's about what you can figure out.",
"The only way to go fast is to go well.",
"Clean code always looks like it was written by someone who cares.",
"Truth can only be found in one place: the code."
];
function startGame() {
if (isPlaying) return;
isPlaying = true;
time = 0;
wpm = 0;
accuracy = 0;
correctChars = 0;
totalChars = 0;
currentCharIndex = 0;
timeElement.textContent = "0s";
wpmElement.textContent = "0";
accuracyElement.textContent = "0%";
progressBar.style.width = "0%";
startBtn.style.display = 'none';
newQuoteBtn.style.display = 'none';
quoteInput.value = '';
quoteInput.disabled = false;
quoteInput.focus();
timer = setInterval(() => {
time++;
timeElement.textContent = `${time}s`;
}, 1000);
getNewQuote();
}
function getNewQuote() {
const randomIndex = Math.floor(Math.random() * quotes.length);
currentQuote = quotes[randomIndex];
quoteDisplay.innerHTML = '';
totalChars = currentQuote.length;
currentQuote.split('').forEach((character, index) => {
const characterSpan = document.createElement('span');
characterSpan.innerText = character;
if (index === 0) {
characterSpan.classList.add('current');
}
quoteDisplay.appendChild(characterSpan);
});
}
function checkText() {
if (!isPlaying) return;
const arrayQuote = quoteDisplay.querySelectorAll('span');
const arrayValue = quoteInput.value.split('');
arrayQuote.forEach(span => span.classList.remove('current'));
let correct = true;
let newCorrectChars = 0;
arrayQuote.forEach((characterSpan, index) => {
const character = arrayValue[index];
if (character == null) {
characterSpan.classList.remove('correct');
characterSpan.classList.remove('incorrect');
correct = false;
if (index === arrayValue.length && index < arrayQuote.length) {
characterSpan.classList.add('current');
currentCharIndex = index;
}
}
else if (character === characterSpan.innerText) {
characterSpan.classList.add('correct');
characterSpan.classList.remove('incorrect');
newCorrectChars++;
if (index === arrayValue.length - 1 && index + 1 < arrayQuote.length) {
arrayQuote[index + 1].classList.add('current');
currentCharIndex = index + 1;
}
}
else {
characterSpan.classList.remove('correct');
characterSpan.classList.add('incorrect');
correct = false;
if (index === arrayValue.length - 1 && index < arrayQuote.length) {
characterSpan.classList.add('current');
currentCharIndex = index;
}
}
});
correctChars = newCorrectChars;
accuracy = Math.round((correctChars / totalChars) * 100);
accuracyElement.textContent = `${accuracy}%`;
const progress = (arrayValue.length / totalChars) * 100;
progressBar.style.width = `${progress}%`;
if (correct) {
finishGame();
}
}
function calculateWPM() {
const words = currentQuote.split(' ').length;
const minutes = time / 60;
wpm = Math.round(words / minutes);
return wpm;
}
function finishGame() {
clearInterval(timer);
isPlaying = false;
quoteInput.disabled = true;
wpm = calculateWPM();
accuracy = Math.round((correctChars / totalChars) * 100);
wpmElement.textContent = wpm;
accuracyElement.textContent = `${accuracy}%`;
showResults();
newQuoteBtn.style.display = 'inline-block';
}
function showResults() {
resultWpm.textContent = wpm;
resultAccuracy.textContent = `${accuracy}%`;
resultTime.textContent = `${time}s`;
resultProgressBar.style.width = `${accuracy}%`;
if (accuracy === 100 && wpm > 60) {
feedbackMessage.textContent = "Amazing! You're a typing wizard! π";
feedbackMessage.style.color = "var(--success)";
} else if (accuracy >= 90 && wpm > 40) {
feedbackMessage.textContent = "Great job! You're getting really good at this! π";
feedbackMessage.style.color = "var(--success)";
} else if (accuracy >= 80) {
feedbackMessage.textContent = "Good work! Keep practicing to improve your speed and accuracy! πͺ";
feedbackMessage.style.color = "var(--accent)";
} else if (accuracy >= 60) {
feedbackMessage.textContent = "Not bad! Focus on accuracy first, then speed will come. π";
feedbackMessage.style.color = "var(--warning)";
} else {
feedbackMessage.textContent = "Keep practicing! Try to focus on accuracy before speed. ποΈββοΈ";
feedbackMessage.style.color = "var(--danger)";
}
resultModal.style.display = "flex";
}
function closeResults() {
resultModal.style.display = "none";
}
startBtn.addEventListener('click', startGame);
newQuoteBtn.addEventListener('click', startGame);
quoteInput.addEventListener('input', checkText);
closeModalBtn.addEventListener('click', closeResults);
getNewQuote();