Skip to content

Commit 6d3a1cf

Browse files
fix: preserve typing stats across sentences
1 parent fa4ab6d commit 6d3a1cf

1 file changed

Lines changed: 46 additions & 22 deletions

File tree

web-app/js/projects/typing-speed-tester.js

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,15 @@ function initTypingSpeedTester() {
255255
let startTime = null;
256256
let timerInterval = null;
257257
let isRunning = false;
258-
let totalCorrect = 0;
259-
let totalIncorrect = 0;
258+
259+
// Session-wide totals (persist across sentences, reset only when a new test starts)
260+
let sessionCorrect = 0;
261+
let sessionIncorrect = 0;
262+
263+
// Per-sentence totals (reset every time a new sentence is generated)
264+
let currentCorrect = 0;
265+
let currentIncorrect = 0;
266+
260267
let sentencesCompleted = 0;
261268

262269
function renderSentence(sentence) {
@@ -283,9 +290,9 @@ function initTypingSpeedTester() {
283290
typingInput.disabled = false;
284291
typingInput.focus();
285292

286-
// Reset current test stats
287-
totalCorrect = 0;
288-
totalIncorrect = 0;
293+
// Only reset the CURRENT sentence counters — session totals stay intact
294+
currentCorrect = 0;
295+
currentIncorrect = 0;
289296
updateStats();
290297

291298
if (resultPanel) {
@@ -294,21 +301,24 @@ function initTypingSpeedTester() {
294301
}
295302

296303
function updateStats() {
297-
const total = totalCorrect + totalIncorrect;
298-
const accuracy = total > 0 ? Math.round((totalCorrect / total) * 100) : 0;
299-
if (statWpm) statWpm.textContent = '0';
304+
const combinedCorrect = sessionCorrect + currentCorrect;
305+
const combinedIncorrect = sessionIncorrect + currentIncorrect;
306+
const total = combinedCorrect + combinedIncorrect;
307+
const accuracy = total > 0 ? Math.round((combinedCorrect / total) * 100) : 0;
308+
if (statWpm) statWpm.textContent = calculateWPM().toString();
300309
if (statAccuracy) statAccuracy.textContent = accuracy + '%';
301-
if (statCorrect) statCorrect.textContent = totalCorrect;
302-
if (statIncorrect) statIncorrect.textContent = totalIncorrect;
310+
if (statCorrect) statCorrect.textContent = combinedCorrect;
311+
if (statIncorrect) statIncorrect.textContent = combinedIncorrect;
303312
if (accuracyDisplay) accuracyDisplay.textContent = accuracy + '%';
304-
if (errorsDisplay) errorsDisplay.textContent = totalIncorrect;
313+
if (errorsDisplay) errorsDisplay.textContent = combinedIncorrect;
305314
}
306315

307316
function calculateWPM() {
308317
if (!startTime) return 0;
309318
const elapsedMinutes = (Date.now() - startTime) / 60000;
310319
if (elapsedMinutes <= 0) return 0;
311-
const words = totalCorrect / 5; // Standard: 5 chars = 1 word
320+
const combinedCorrect = sessionCorrect + currentCorrect;
321+
const words = combinedCorrect / 5; // Standard: 5 chars = 1 word
312322
return Math.round(words / elapsedMinutes);
313323
}
314324

@@ -348,17 +358,23 @@ function initTypingSpeedTester() {
348358
typingInput.disabled = true;
349359
isRunning = false;
350360

361+
// Fold whatever was typed in the current (incomplete) sentence into the session totals
362+
sessionCorrect += currentCorrect;
363+
sessionIncorrect += currentIncorrect;
364+
currentCorrect = 0;
365+
currentIncorrect = 0;
366+
351367
const wpm = calculateWPM();
352-
const total = totalCorrect + totalIncorrect;
353-
const accuracy = total > 0 ? Math.round((totalCorrect / total) * 100) : 0;
368+
const total = sessionCorrect + sessionIncorrect;
369+
const accuracy = total > 0 ? Math.round((sessionCorrect / total) * 100) : 0;
354370

355371
const resultsHtml = `
356372
<div style="text-align: center;">
357373
<div style="font-size: 2rem; margin-bottom: 1rem;">${isComplete ? '🎉' : '⏰'}</div>
358374
<div><strong>Final WPM:</strong> ${wpm}</div>
359375
<div><strong>Accuracy:</strong> ${accuracy}%</div>
360-
<div><strong>Correct Characters:</strong> ${totalCorrect}</div>
361-
<div><strong>Incorrect Characters:</strong> ${totalIncorrect}</div>
376+
<div><strong>Correct Characters:</strong> ${sessionCorrect}</div>
377+
<div><strong>Incorrect Characters:</strong> ${sessionIncorrect}</div>
362378
<div><strong>Sentences Completed:</strong> ${sentencesCompleted}</div>
363379
<div><strong>Difficulty:</strong> ${difficultyConfig[selectedDifficulty].label}</div>
364380
</div>
@@ -407,13 +423,17 @@ function initTypingSpeedTester() {
407423
}
408424
}
409425

410-
totalCorrect = correct;
411-
totalIncorrect = incorrect;
426+
// These reflect ONLY the sentence currently being typed
427+
currentCorrect = correct;
428+
currentIncorrect = incorrect;
412429
updateStats();
413430
updateLiveStats();
414431

415432
// Check if sentence is complete
416433
if (typed.length >= sentence.length) {
434+
// Fold this sentence's results into the running session totals
435+
sessionCorrect += currentCorrect;
436+
sessionIncorrect += currentIncorrect;
417437
sentencesCompleted++;
418438
generateSentence();
419439
}
@@ -425,8 +445,10 @@ function initTypingSpeedTester() {
425445
resultStage.classList.add('hidden');
426446

427447
sentencesCompleted = 0;
428-
totalCorrect = 0;
429-
totalIncorrect = 0;
448+
sessionCorrect = 0;
449+
sessionIncorrect = 0;
450+
currentCorrect = 0;
451+
currentIncorrect = 0;
430452
startTime = Date.now();
431453
isRunning = false;
432454

@@ -454,8 +476,10 @@ function initTypingSpeedTester() {
454476
gameStage.classList.remove('hidden');
455477

456478
sentencesCompleted = 0;
457-
totalCorrect = 0;
458-
totalIncorrect = 0;
479+
sessionCorrect = 0;
480+
sessionIncorrect = 0;
481+
currentCorrect = 0;
482+
currentIncorrect = 0;
459483
startTime = Date.now();
460484
isRunning = false;
461485

0 commit comments

Comments
 (0)