-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
301 lines (246 loc) · 9.83 KB
/
index.js
File metadata and controls
301 lines (246 loc) · 9.83 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
const quoteApiUrl = "https://zenquotes.io/api/random";
const quoteSection = document.getElementById("quote");
const userInput = document.getElementById("quote-input");
let quote = "";
let time = 60;
let timer = null;
let mistakes = 0;
// -------------------- Difficulty Quotes --------------------
const easyQuotes = [
"The quick brown fox jumps over the lazy dog.",
"Practice makes progress.",
"Focus on accuracy first.",
"Slow typing builds speed.",
"Stay calm and keep typing.",
"Good habits create results.",
"Every key press matters.",
"Typing is a useful skill.",
"Small steps lead forward.",
"Consistency brings success.",
"Accuracy beats speed.",
"Relax and type carefully.",
"Learning takes patience.",
"Practice every single day.",
"Mistakes help you improve."
];
const mediumQuotes = [
"Typing speed improves naturally when accuracy becomes consistent.",
"Small improvements repeated daily create strong long term skills.",
"Discipline will take you further than motivation ever can.",
"Focus on clean input instead of rushing the clock.",
"Accuracy today leads to speed tomorrow.",
"Good habits build reliable performance over time.",
"Progress feels slow until it suddenly becomes obvious.",
"Typing is a rhythm that improves with steady practice.",
"Staying relaxed helps reduce unnecessary mistakes.",
"Repetition builds muscle memory and confidence.",
"Speed grows when mistakes are controlled.",
"Calm focus produces better typing results.",
"Consistency matters more than raw talent.",
"Correct practice leads to lasting improvement.",
"Skills compound quietly before they show results."
];
const hardQuotes = [
"Consistency compounds silently until the accumulated effort produces undeniable results.",
"True improvement comes from deliberate practice focused on precision rather than speed.",
"Mastery is built through disciplined repetition of correct technique over extended periods.",
"High performance typing requires calm focus, controlled breathing, and deliberate accuracy.",
"Speed achieved without accuracy creates instability and reinforces incorrect muscle memory.",
"Sustainable progress depends on patience, structure, and consistent feedback loops.",
"Advanced skill development demands attention to detail under increasing cognitive load.",
"Efficiency emerges when effort is reduced through optimized movement and awareness.",
"Complex skills improve fastest when fundamentals are reinforced under pressure.",
"Long term excellence is the product of systems, not bursts of motivation.",
"Training slowly with precision produces faster and more reliable execution.",
"Cognitive clarity improves when distractions are removed and focus is sustained.",
"Expert performance results from thousands of small correct decisions.",
"Stress reveals the quality of your preparation.",
"Control under pressure separates advanced practitioners from beginners."
];
let currentDifficulty = "medium"; // "easy" | "medium" | "hard"
// -------------------- XP + Levels --------------------
let xp = 0;
let level = 1;
function xpToNextLevel(lvl) {
return 100 + (lvl - 1) * 40; // 100, 140, 180...
}
function updateLevelUI() {
const levelEl = document.getElementById("level");
const xpEl = document.getElementById("xp");
const xpNextEl = document.getElementById("xp-next");
const xpGainedEl = document.getElementById("xp-gained");
if (levelEl) levelEl.innerText = level;
if (xpEl) xpEl.innerText = xp;
if (xpNextEl) xpNextEl.innerText = xpToNextLevel(level);
if (xpGainedEl) xpGainedEl.innerText = ""; // clear per run
}
function difficultyMultiplier() {
if (currentDifficulty === "easy") return 1.0;
if (currentDifficulty === "hard") return 1.6;
return 1.3; // medium
}
function addXP(wpm, accuracy) {
const base = (wpm * 1.2) + (accuracy * 0.8);
const accuracyFactor = accuracy < 70 ? 0.5 : accuracy < 85 ? 0.8 : 1.0;
const gained = Math.max(
0,
Math.round(base * difficultyMultiplier() * accuracyFactor)
);
xp += gained;
while (xp >= xpToNextLevel(level)) {
xp -= xpToNextLevel(level);
level++;
}
localStorage.setItem("xp", String(xp));
localStorage.setItem("level", String(level));
updateLevelUI();
const xpGainedEl = document.getElementById("xp-gained");
if (xpGainedEl) xpGainedEl.innerText = "+" + gained + " XP";
return gained;
}
// -------------------- Difficulty handling --------------------
function getQuoteByDifficulty() {
if (currentDifficulty === "easy") return easyQuotes;
if (currentDifficulty === "hard") return hardQuotes;
return mediumQuotes;
}
function renderQuoteToScreen(text) {
quote = text;
quoteSection.innerHTML = quote
.split("")
.map(ch => `<span class="quote-chars">${ch}</span>`)
.join("");
}
// Set difficulty (NO event.target bugs)
function setDifficulty(level) {
currentDifficulty = level;
// highlight active (if you added .difficulty buttons)
document.querySelectorAll(".difficulty button").forEach(btn => {
btn.classList.toggle("active", btn.dataset.level === level);
});
// reset state
clearInterval(timer);
timer = null;
time = 60;
mistakes = 0;
document.getElementById("mistakes").innerText = "0";
document.getElementById("timer").innerText = "0s";
document.querySelector(".result").style.display = "none";
userInput.value = "";
userInput.disabled = true;
document.getElementById("start-test").style.display = "block";
document.getElementById("stop-test").style.display = "none";
renderNewQuote();
}
// -------------------- Quote loader (API then fallback) --------------------
const renderNewQuote = async () => {
try {
quoteSection.innerHTML = "Loading...";
const response = await fetch(quoteApiUrl);
if (!response.ok) throw new Error("HTTP " + response.status);
const data = await response.json();
const apiQuote = data?.[0]?.q;
if (!apiQuote) throw new Error("Bad API format");
renderQuoteToScreen(apiQuote);
} catch (err) {
console.error("Quote fetch failed:", err);
const quotes = getQuoteByDifficulty();
const fallback = quotes[Math.floor(Math.random() * quotes.length)];
renderQuoteToScreen(fallback);
}
};
// -------------------- Typing logic --------------------
userInput.addEventListener("input", () => {
const quoteChars = Array.from(document.querySelectorAll(".quote-chars"));
let userInputChars = userInput.value.split("");
if (userInputChars.length > quoteChars.length) {
userInputChars = userInputChars.slice(0, quoteChars.length);
userInput.value = userInputChars.join("");
}
quoteChars.forEach((char, index) => {
const typedChar = userInputChars[index];
if (typedChar == null) {
char.classList.remove("success", "fail");
return;
}
if (char.innerText === typedChar) {
char.classList.add("success");
char.classList.remove("fail");
} else {
if (!char.classList.contains("fail")) {
mistakes++;
document.getElementById("mistakes").innerText = String(mistakes);
}
char.classList.add("fail");
char.classList.remove("success");
}
});
if (quoteChars.length > 0 && quoteChars.every(el => el.classList.contains("success"))) {
displayResult();
}
});
// -------------------- Timer --------------------
function updateTimer() {
if (time === 0) {
displayResult();
} else {
document.getElementById("timer").innerText = --time + "s";
}
}
const timeReduce = () => {
clearInterval(timer);
time = 60;
document.getElementById("timer").innerText = "60s";
timer = setInterval(updateTimer, 1000);
};
// -------------------- End test --------------------
const displayResult = () => {
document.querySelector(".result").style.display = "block";
clearInterval(timer);
timer = null;
document.getElementById("stop-test").style.display = "none";
userInput.disabled = true;
const secondsTaken = 60 - time;
const minutesTaken = Math.max(secondsTaken / 60, 1 / 60);
const wpm = Number((userInput.value.length / 5 / minutesTaken).toFixed(2));
document.getElementById("wpm").innerText = wpm + " wpm";
const typedLen = userInput.value.length;
const accuracy = typedLen > 0
? Math.max(0, Math.round(((typedLen - mistakes) / typedLen) * 100))
: 0;
document.getElementById("accuracy").innerText = accuracy + "%";
addXP(wpm, accuracy);
};
// -------------------- Start test --------------------
const startTest = () => {
mistakes = 0;
document.getElementById("mistakes").innerText = "0";
clearInterval(timer);
timer = null;
userInput.disabled = false;
userInput.value = "";
document.querySelector(".result").style.display = "none";
renderNewQuote();
timeReduce();
document.getElementById("start-test").style.display = "none";
document.getElementById("stop-test").style.display = "block";
};
// -------------------- On load --------------------
window.onload = () => {
userInput.value = "";
userInput.disabled = true;
document.getElementById("timer").innerText = "0s";
document.getElementById("mistakes").innerText = "0";
document.getElementById("start-test").style.display = "block";
document.getElementById("stop-test").style.display = "none";
document.querySelector(".result").style.display = "none";
// load saved XP/level
xp = Number(localStorage.getItem("xp")) || 0;
level = Number(localStorage.getItem("level")) || 1;
updateLevelUI();
// If you added difficulty buttons with data-level, set default highlight
document.querySelectorAll(".difficulty button").forEach(btn => {
btn.classList.toggle("active", btn.dataset.level === currentDifficulty);
});
renderNewQuote();
};