Merge pull request #3031 from jspsych/fix-datacolumn-mean-2905

Change `DataColumn.mean()` to ignore `null` and `undefined` values
This commit is contained in:
Josh de Leeuw 2024-06-14 17:50:03 -04:00 committed by GitHub
commit c3eb8e54c0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 1 deletions

View File

@ -0,0 +1,5 @@
---
"jspsych": major
---
Changed the behavior of `DataColumn.mean()` to exclude `null` and `undefined` values from the calculation, as suggested in #2905

View File

@ -10,7 +10,18 @@ export class DataColumn {
} }
mean() { mean() {
return this.sum() / this.count(); let sum = 0;
let count = 0;
for (const value of this.values) {
if (typeof value !== "undefined" && value !== null) {
sum += value;
count++;
}
}
if (count === 0) {
return undefined;
}
return sum / count;
} }
median() { median() {