-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
191 lines (168 loc) · 8.15 KB
/
main.js
File metadata and controls
191 lines (168 loc) · 8.15 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
document.addEventListener("DOMContentLoaded", () => {
const appStart = document.querySelector('#appStart');
const coverPage = document.querySelector('#coverPage');
const bmiInputSection = document.querySelector('#bmiInputSection');
const resultSection = document.querySelector('#resultSection');
const bmiForm = document.getElementById("bmiForm");
const submitBtn = document.getElementById("submitBtn");
const refreshBtn = document.getElementById("refresh");
const bmiValueText = document.getElementById("bmiValue");
const bmiProgressBar = document.getElementById("bmiProgressBar");
const healthyWeightRange = document.getElementById("healthyWeightRange");
const bmiPrimeText = document.getElementById("bmiPrime");
const ponderalIndexText = document.getElementById("ponderalIndex");
const bmiRangeText = document.getElementById("bmiRange");
appStart.addEventListener('click', () => {
coverPage.classList.add('hidden');
bmiInputSection.classList.remove('hidden');
});
// Weight Unit Toggle
let unitWeight = document.querySelector('#unitWeight');
unitWeight.addEventListener('change', () => {
const selectedWeightUnit = document.getElementById("unitWeight").value;
const weightKgInput = document.getElementById("weightKg");
const weightPoundInput = document.getElementById("weightPound");
if (selectedWeightUnit === "kg") {
weightKgInput.classList.remove("hidden");
weightPoundInput.classList.add("hidden");
} else if (selectedWeightUnit === "lb") {
weightKgInput.classList.add("hidden");
weightPoundInput.classList.remove("hidden");
}
});
// Height Unit Toggle
let unit = document.querySelector('#unit');
unit.addEventListener('change', () => {
const selectedUnit = document.getElementById("unit").value;
const heightCmInput = document.getElementById("heightCm");
const heightFeetInput = document.getElementById("heightFeet");
const heightInchesInput = document.getElementById("heightInches");
if (selectedUnit === "cm") {
heightCmInput.classList.remove("hidden");
heightFeetInput.classList.add("hidden");
heightInchesInput.classList.add("hidden");
} else if (selectedUnit === "in") {
heightCmInput.classList.add("hidden");
heightFeetInput.classList.remove("hidden");
heightInchesInput.classList.remove("hidden");
}
});
// Submit Button Event Handling
submitBtn.addEventListener("click", (e) => {
e.preventDefault(); // Prevent form submission and page reload
if (validateForm()) {
calculateBMI();
}
});
refreshBtn.addEventListener("click", (e) => {
e.preventDefault(); // Prevent form submission and page reload
bmiInputSection.classList.remove('hidden');
resultSection.classList.add('hidden');
bmiForm.reset(); // Reset the form values
resetErrors(); // Reset error messages
});
function validateForm() {
let isValid = true;
const age = document.getElementById('age').value;
const gender = document.querySelector('input[name="gender"]:checked');
const heightCm = document.getElementById('heightCm').value;
const heightFeet = document.getElementById('heightFeet').value;
const heightInches = document.getElementById('heightInches').value;
const weightKg = document.getElementById('weightKg').value;
const weightPound = document.getElementById('weightPound').value;
resetErrors();
if (!gender) {
document.getElementById('genderError').classList.remove('hidden');
isValid = false;
}
if (!age) {
document.getElementById('ageError').classList.remove('hidden');
isValid = false;
}
if (document.getElementById("unit").value === 'cm' && !heightCm) {
document.getElementById('heightError').classList.remove('hidden');
isValid = false;
}
if (document.getElementById("unit").value === 'in' && (!heightFeet || !heightInches)) {
document.getElementById('heightError').classList.remove('hidden');
isValid = false;
}
if (document.getElementById("unitWeight").value === 'kg' && !weightKg) {
document.getElementById('weightError').classList.remove('hidden');
isValid = false;
}
if (document.getElementById("unitWeight").value === 'lb' && !weightPound) {
document.getElementById('weightError').classList.remove('hidden');
isValid = false;
}
return isValid;
}
function resetErrors() {
document.getElementById('genderError').classList.add('hidden');
document.getElementById('ageError').classList.add('hidden');
document.getElementById('heightError').classList.add('hidden');
document.getElementById('weightError').classList.add('hidden');
}
function calculateBMI() {
const age = document.getElementById('age').value;
const gender = document.querySelector('input[name="gender"]:checked').value;
const heightUnit = document.getElementById('unit').value;
const weightUnit = document.getElementById('unitWeight').value;
let height;
if (heightUnit === 'cm') {
height = document.getElementById('heightCm').value / 100; // convert to meters
} else {
const heightFeet = document.getElementById('heightFeet').value;
const heightInches = document.getElementById('heightInches').value;
height = (heightFeet * 0.3048) + (heightInches * 0.0254); // convert to meters
}
let weight;
if (weightUnit === 'kg') {
weight = document.getElementById('weightKg').value;
} else {
weight = document.getElementById('weightPound').value * 0.453592; // convert to kg
}
const bmi = (weight / (height * height)).toFixed(1);
const bmiPrime = (bmi / 24.9).toFixed(2);
const ponderalIndex = (weight / (height ** 3)).toFixed(2);
const healthyWeightLow = (18.5 * (height * height)).toFixed(1);
const healthyWeightHigh = (24.9 * (height * height)).toFixed(1);
let category;
if (bmi <= 18.4) {
category = "Underweight";
} else if (bmi >= 18.5 && bmi <= 24.9) {
category = "Normal weight";
} else if (bmi >= 25 && bmi <= 29.9) {
category = "Overweight";
} else {
category = "Obese";
}
// Update result section
bmiValueText.textContent = bmi;
healthyWeightRange.textContent = `${healthyWeightLow}kg - ${healthyWeightHigh}kg`;
bmiPrimeText.textContent = bmiPrime;
ponderalIndexText.textContent = ponderalIndex;
// Update BMI Range text dynamically
bmiRangeText.textContent = `Healthy BMI Range: 18.5 - 24.9 (${category})`;
// Update progress bar
const progressBarWidth = Math.min((bmi / 40) * 100, 100); // Assuming 40 is the max BMI shown
bmiProgressBar.style.width = `${progressBarWidth}%`;
// Change progress bar color based on BMI category
if (category === "Underweight") {
bmiProgressBar.classList.remove('bg-green-500', 'bg-yellow-500', 'bg-red-500');
bmiProgressBar.classList.add('bg-blue-500');
} else if (category === "Normal weight") {
bmiProgressBar.classList.remove('bg-blue-500', 'bg-yellow-500', 'bg-red-500');
bmiProgressBar.classList.add('bg-green-500');
} else if (category === "Overweight") {
bmiProgressBar.classList.remove('bg-green-500', 'bg-blue-500', 'bg-red-500');
bmiProgressBar.classList.add('bg-yellow-500');
} else {
bmiProgressBar.classList.remove('bg-green-500', 'bg-yellow-500', 'bg-blue-500');
bmiProgressBar.classList.add('bg-red-500');
}
// Display result section and hide input section
bmiInputSection.classList.add('hidden');
resultSection.classList.remove('hidden');
}
});