-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
51 lines (42 loc) · 1.63 KB
/
Copy pathscript.js
File metadata and controls
51 lines (42 loc) · 1.63 KB
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
const userName = localStorage.getItem("username");
const target = parseInt(localStorage.getItem("target"));
const submitButton = document.getElementById("submit-button");
const resetButton = document.getElementById('reset');
let answer = document.getElementById('answer');
let needed = 0;
if (!userName) {
window.location.href = "index.html";
} else {
document.getElementById("first-name").textContent = `${userName}'s homepage`;
}
document.getElementById('calculator').addEventListener("submit", (e) => {
e.preventDefault();
const carbs = parseInt(document.getElementById('carbs').value);
const currentBs = parseInt(document.getElementById('insulin').value);
if (currentBs <= 70) {
answer.style.color = "red";
answer.textContent = "Blood sugar is too low- do not take any insulin";
return;
} else if (currentBs <= 90) {
answer.style.color = "yellow";
} else {
answer.style.color = "#43cea2";
}
let insulinForCarbs = 0;
if (carbs >= 10 || currentBs > target) {
insulinForCarbs = Math.floor(carbs / document.getElementById('ratio').value); // 1 unit per 14g
}
let correction = (currentBs - target) / 50;
if (correction < 0) correction = 0;
let totalInsulin = Math.round((insulinForCarbs + correction) * 2) / 2;
if (currentBs < target) {
needed = Math.floor((target - currentBs) / 50);
totalInsulin -= needed;
}
answer.textContent = `${totalInsulin} is the correct amount`;
});
resetButton.addEventListener('click', () => {
localStorage.removeItem("username");
localStorage.removeItem("target");
location.reload();
})