diff --git a/src/js/db.js b/src/js/db.js index 55d1be143..4ba0e7ba0 100644 --- a/src/js/db.js +++ b/src/js/db.js @@ -212,23 +212,46 @@ export async function getUserAverageWpm10( function cont() { let wpmSum = 0; let count = 0; + let last10Wpm = 0; + let last10Count = 0; // You have to use every so you can break out of the loop dbSnapshot.results.every((result) => { if ( result.mode == mode && - result.mode2 == mode2 && result.punctuation == punctuation && result.language == language && result.difficulty == difficulty ) { - wpmSum += result.wpm; - count++; - if (count >= 10) { - return false; + // Continue if the mode2 doesn't match unless it's a quote. + if (result.mode2 != mode2 && mode != "quote") { + return true; + } + + // Grab the most recent 10 wpm's for the current mode. + if (last10Count < 10) { + last10Wpm += result.wpm; + last10Count++; + } + + // Check mode2 matches and append, for quotes this is the quote id. + if (result.mode2 == mode2) { + wpmSum += result.wpm; + count++; + if (count >= 10) { + // Break out of every loop since we a maximum of the last 10 wpm results. + return false; + } } } return true; }); + + // Return the last 10 average wpm for quote if the current quote id has never been completed before by the user. + if (count == 0 && mode == "quote") { + return Math.round(last10Wpm / last10Count); + } + + // Return the average wpm of the last 10 completions for the targeted test mode. return Math.round(wpmSum / count); }