Addition of the defaultValue property

This commit is contained in:
Jaap van Ekris 2024-01-24 15:45:45 +01:00 committed by GitHub
parent cb387a8121
commit 02fa5293f2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 16 additions and 5 deletions

View File

@ -2,13 +2,13 @@
/*
Open Rowing Monitor, https://github.com/laberning/openrowingmonitor
This creates a series with weights
It allows for determining the Average, Weighed Average, Median, Number of Positive, number of Negative
This creates a series with a maximum number of values
It allows for determining the Average, Median, Number of Positive, number of Negative
*/
import { createSeries } from './Series.js'
function createWeighedSeries (maxSeriesLength) {
function createWeighedSeries (maxSeriesLength, defaultValue) {
const dataArray = createSeries(maxSeriesLength)
const weightArray = createSeries(maxSeriesLength)
const weightedArray = createSeries(maxSeriesLength)
@ -48,14 +48,20 @@ function createWeighedSeries (maxSeriesLength) {
}
function average () {
return dataArray.average()
if (dataArray.length() > 0) {
// The series contains sufficient values to be valid
return dataArray.average()
} else {
// The array isn't sufficiently filled
return defaultValue
}
}
function weighedAverage () {
if (dataArray.length() > 0 && weightArray.sum() !== 0) {
return (weightedArray.sum() / weightArray.sum())
} else {
return undefined
return defaultValue
}
}
@ -71,6 +77,10 @@ function createWeighedSeries (maxSeriesLength) {
return dataArray.median()
}
function reliable () {
return dataArray.length() > 0
}
function series () {
return dataArray.series()
}
@ -96,6 +106,7 @@ function createWeighedSeries (maxSeriesLength) {
maximum,
median,
series,
reliable,
reset
}
}