-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbac_calculator.js
77 lines (53 loc) · 2.17 KB
/
bac_calculator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Function to calculate blood alcohol concentration (BAC)
function calculateBAC() {
const startDrinkingTime = new Date(getFirstDrinkTime().getTime()).addHours(-1);
var soberInterval = new Date(0);
// console.log(startDrinkingTime);
// Initialize arrays for chart data
const labels = [];
const bacData = [];
// d is the density of alcohol (= 0.789 grams per millilitre, constant)
const d = 0.789458;
// a is the proportion of the alcohol absorbed
const a = 0.95; // we assume to absorb 95% of the alcohol
// BMI is the Body Mass Index
let BMI = weight / Math.pow(height, 2);
// r is the subject's proportion of body water in litres/kilogram, divided by the proportion of water in blood in litres/litre (Widmark Factor)
var r;
if (biologicalSex == "Men" || biologicalSex == "Attack helicopter") {
r = 1.0181 - 0.01213 * BMI;
}
else if (biologicalSex == "Women") {
r = 0.9367 - 0.01240 * BMI;
}
// M is the mass of the subject, in kilograms
M = weight;
// B is the subject's elimination rate, in mg% per hour
const B = 14.8; // can be an aproximation
// calculate each point of the graph for different times
let i = startDrinkingTime;
while ( i < addDates(startDrinkingTime, chartLength) ) {
const alcoholMassSum = drinks.reduce((sum, drink) => {
const drinkAlcoholMass = drink.volume * drink.alcohol/100 * d;
return sum + (drink.drinkTime.greaterThan(i) ? 0 : drinkAlcoholMass);
}, 0); // UNIT : mL * % * g/mL = g
// time is the duration from the start of the session to the relevant time, in hours
const time = (i-startDrinkingTime)/(60*60*1000);
// console.log("time=", time, "soberInterval=", soberInterval, "new_time=", new_time);
// BAC FORMULA :
const C = (1/100)*(((100*alcoholMassSum*a)/(r*M)-B*(time - soberInterval.toHours()))).toFixed(3); //(1/100)*mg% => g/L
var bac;
if (C > 0) {
bac = C;
}
else {
bac = 0;
soberInterval = addDates(soberInterval, interval);
}
// console.log("bac=", bac);
labels.push(i.toTimeString().split(' ')[0].slice(0, 5));
bacData.push(bac);
i = addDates(i, interval);
}
return { labels, bacData };
}