Skip to content

Commit

Permalink
correct calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
T-Damer committed Mar 29, 2024
1 parent 2d2db6c commit 3c10f66
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
29 changes: 20 additions & 9 deletions src/helpers/calculateCompletedVacs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// On image we have 20 sectors with different date gaps (for example first sector is a gap from 0 to 30 days)
// We calculate time since birth in days and put it in a specific sector
// E.g. `daysFromBirth = 21`, it falls into 1st sector - [0, 30]
// we calculate `sectorCompletion` by doing (21 - 0) / 30 === 0.7
// We return 0.7% + leftDeadSpace as a width for css grayscale styles

import isBetween from 'helpers/isBetween'

const ONE_DAY = 1000 * 60 * 60 * 24

Expand All @@ -11,30 +15,37 @@ export default function (birthDate: number) {
let currentSector = 0
for (const [index, gap] of gaps.entries()) {
if (isBetween(daysFromBirth, gap[0], gap[1])) {
const sectorCompletion = daysFromBirth / gap[1]
const sectorCompletion = (daysFromBirth - gap[0]) / gap[1] // e.g. 34 days = (34 - 31) / 60 = 0.05 in gap [31, 60]

currentSector = index + sectorCompletion
}
}

// CSS stuff, I need to know how much space on left should be colored
const leftDeadZone = 10 // table legend
const leftDeadZone = 10 // table legend on the left

const amountOfSectors = 20
const oneSector = (100 - leftDeadZone) / amountOfSectors // 4.5% for one sector, used for css styles
const coloredSectorsPercent = currentSector * oneSector

const completedPercent = leftDeadZone + coloredSectorsPercent

return { completedPercent, daysFromBirth }
}
const olderThan60 = completedPercent > 95 ? 100 : completedPercent // edge case because we start index from 0

console.log('---- debug data ----')
console.log([
currentSector,
oneSector,
coloredSectorsPercent,
completedPercent,
olderThan60,
])

function isBetween(number: number, first: number, second: number) {
return (number - first) * (number - second) <= 0
return { completedPercent: olderThan60, daysFromBirth }
}

const gaps: [number, number][] = [
[0, 30],
[31, 60],
[0, 7],
[8, 60],
[61, 90],
[91, 120],
[121, 180],
Expand Down
7 changes: 7 additions & 0 deletions src/helpers/isBetween.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function isBetween(
number: number,
first: number,
second: number
) {
return (number - first) * (number - second) <= 0
}

0 comments on commit 3c10f66

Please sign in to comment.