rounds period stats, adds tests
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
import assert from 'assert'
|
||||
|
||||
export default function getPeriodLengthStats(cycleLengths) {
|
||||
throwIfArgsAreNotInRequiredFormat(cycleLengths)
|
||||
const periodLengthStats = {}
|
||||
const sortedCycleLengths = cycleLengths.sort((a, b) => {
|
||||
return a - b
|
||||
})
|
||||
periodLengthStats.minimum = sortedCycleLengths[0]
|
||||
periodLengthStats.maximum = sortedCycleLengths[cycleLengths.length - 1]
|
||||
periodLengthStats.mean = Math.round(
|
||||
cycleLengths.reduce(getSum) / cycleLengths.length * 100) / 100
|
||||
// median
|
||||
if (cycleLengths.length % 2 == 1) {
|
||||
periodLengthStats.median = sortedCycleLengths[(cycleLengths.length + 1) /
|
||||
2 - 1]
|
||||
}
|
||||
else {
|
||||
const middle = cycleLengths.length / 2
|
||||
periodLengthStats.median = (sortedCycleLengths[middle - 1] +
|
||||
sortedCycleLengths[middle]) / 2
|
||||
}
|
||||
// corrected standard deviation (based on unbiased sample variance)
|
||||
periodLengthStats.stdDeviation = null // for case t
|
||||
if (cycleLengths.length > 1) {
|
||||
const sumOfSquares = cycleLengths.map(cycleLength => {
|
||||
return Math.pow(cycleLength - periodLengthStats.mean, 2)
|
||||
}).reduce(getSum)
|
||||
periodLengthStats.stdDeviation = Math.round(
|
||||
Math.sqrt(sumOfSquares / (cycleLengths.length - 1 )) * 100) / 100
|
||||
}
|
||||
return periodLengthStats
|
||||
}
|
||||
|
||||
function getSum(total, num) {
|
||||
return total + num
|
||||
}
|
||||
|
||||
function throwIfArgsAreNotInRequiredFormat(cycleLengths) {
|
||||
assert.ok(Array.isArray(cycleLengths), 'Function requires input to be an array.')
|
||||
assert.ok(cycleLengths.length > 0, 'Input array should not be empty.')
|
||||
cycleLengths.forEach(cycleLength => {
|
||||
assert.equal(typeof cycleLength, 'number', 'Elements in the array should be of type number.')
|
||||
assert.ok(!isNaN(cycleLength), 'Elements of Array should not be NaN.')
|
||||
})
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
export default function getPeriodLengthStats(cycleLengthArray) {
|
||||
if (Array.isArray(cycleLengthArray) && (cycleLengthArray.length > 0) && cycleLengthArray.every(cycleLength => {
|
||||
return (typeof cycleLength === 'number') && !(isNaN(cycleLength))
|
||||
})) {
|
||||
const PeriodLengthStats = {}
|
||||
const sortedCycleLegthArray = cycleLengthArray.sort((a, b) => {
|
||||
return a - b
|
||||
})
|
||||
PeriodLengthStats.minimum = sortedCycleLegthArray[0]
|
||||
PeriodLengthStats.maximum = sortedCycleLegthArray[cycleLengthArray.length - 1]
|
||||
PeriodLengthStats.mean = cycleLengthArray.reduce(getSum) / cycleLengthArray.length
|
||||
// median
|
||||
if (cycleLengthArray.length % 2 == 1) {
|
||||
PeriodLengthStats.median = sortedCycleLegthArray[(cycleLengthArray.length + 1) / 2 - 1]
|
||||
}
|
||||
else {
|
||||
const middle = cycleLengthArray.length / 2
|
||||
PeriodLengthStats.median = (sortedCycleLegthArray[middle - 1] + sortedCycleLegthArray[middle]) / 2
|
||||
}
|
||||
|
||||
PeriodLengthStats.stdDeviation = 0 // default
|
||||
if (cycleLengthArray.length > 1) {
|
||||
const sumOfSquares = cycleLengthArray.map(cycleLength => {
|
||||
return Math.pow(cycleLength - PeriodLengthStats.mean, 2)
|
||||
}).reduce(getSum)
|
||||
PeriodLengthStats.stdDeviation = Math.sqrt(sumOfSquares / (cycleLengthArray.length - 1 ))
|
||||
}
|
||||
return PeriodLengthStats
|
||||
}
|
||||
console.error('getPeriodLengthStats requiers an array of numbers with length > 0.')
|
||||
}
|
||||
|
||||
function getSum(total, num) {
|
||||
return total + num
|
||||
}
|
||||
Reference in New Issue
Block a user