resolves some of the suggested improvements, renames the term period with cycle

This commit is contained in:
tina
2018-08-09 18:31:14 +02:00
parent c99aa7509d
commit f2b8723fb9
4 changed files with 48 additions and 53 deletions
+11 -11
View File
@@ -1,38 +1,38 @@
import assert from 'assert'
export default function getPeriodLengthStats(cycleLengths) {
export default function getCycleLengthStats(cycleLengths) {
throwIfArgsAreNotInRequiredFormat(cycleLengths)
const periodLengthStats = {}
const cycleLengthStats = {}
const sortedCycleLengths = cycleLengths.sort((a, b) => {
return a - b
})
periodLengthStats.minimum = sortedCycleLengths[0]
periodLengthStats.maximum = sortedCycleLengths[cycleLengths.length - 1]
periodLengthStats.mean = Math.round(
cycleLengthStats.minimum = sortedCycleLengths[0]
cycleLengthStats.maximum = sortedCycleLengths[cycleLengths.length - 1]
cycleLengthStats.mean = Math.round(
cycleLengths.reduce(getSum) / cycleLengths.length * 100
) / 100
// median
if (cycleLengths.length % 2 == 1) {
periodLengthStats.median = sortedCycleLengths[
cycleLengthStats.median = sortedCycleLengths[
(cycleLengths.length + 1) / 2 - 1
]
} else {
const middle = cycleLengths.length / 2
periodLengthStats.median = (sortedCycleLengths[middle - 1] +
cycleLengthStats.median = (sortedCycleLengths[middle - 1] +
sortedCycleLengths[middle]) / 2
}
// corrected standard deviation (based on unbiased sample variance)
if (cycleLengths.length > 1) {
const sumOfSquares = cycleLengths.map(cycleLength => {
return Math.pow(cycleLength - periodLengthStats.mean, 2)
return Math.pow(cycleLength - cycleLengthStats.mean, 2)
}).reduce(getSum)
periodLengthStats.stdDeviation = Math.round(
cycleLengthStats.stdDeviation = Math.round(
Math.sqrt(sumOfSquares / (cycleLengths.length - 1 )) * 100
) / 100
} else {
periodLengthStats.stdDeviation = null
cycleLengthStats.stdDeviation = null
}
return periodLengthStats
return cycleLengthStats
}
function getSum(total, num) {