Merge pull request #1238 from dewblackio2/master

Fixed undefined (NaN) issue with Personal Average Pace Caret when using quote mode
This commit is contained in:
Jack 2021-04-12 23:12:30 +01:00 committed by GitHub
commit e0099607ea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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);
}