-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
234 lines (200 loc) · 7.92 KB
/
Copy pathscript.js
File metadata and controls
234 lines (200 loc) · 7.92 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Unit system state
let currentSystem = 'metric';
// Unit toggle functionality
document.querySelectorAll('.unit-btn').forEach(btn => {
btn.addEventListener('click', function() {
// Update active state
document.querySelectorAll('.unit-btn').forEach(b => b.classList.remove('active'));
this.classList.add('active');
// Update current system
currentSystem = this.dataset.system;
// Toggle input visibility
if (currentSystem === 'metric') {
document.querySelector('.metric-input').style.display = 'block';
document.querySelector('.imperial-input').style.display = 'none';
document.getElementById('weight-unit').textContent = 'kg';
} else {
document.querySelector('.metric-input').style.display = 'none';
document.querySelector('.imperial-input').style.display = 'block';
document.getElementById('weight-unit').textContent = 'lbs';
}
// Clear error message
hideError();
});
});
// Calculate BMI
function calculateBMI() {
hideError();
// Get weight
const weight = parseFloat(document.getElementById('weight').value);
if (!weight || weight <= 0) {
showError('Please enter a valid weight');
return;
}
let heightInMeters;
if (currentSystem === 'metric') {
// Get height in cm
const heightCm = parseFloat(document.getElementById('height-cm').value);
if (!heightCm || heightCm <= 0) {
showError('Please enter a valid height');
return;
}
// Convert cm to meters
heightInMeters = heightCm / 100;
} else {
// Get height in feet and inches
const heightFt = parseFloat(document.getElementById('height-ft').value) || 0;
const heightIn = parseFloat(document.getElementById('height-in').value) || 0;
if (heightFt <= 0 && heightIn <= 0) {
showError('Please enter a valid height');
return;
}
// Convert feet and inches to meters
const totalInches = (heightFt * 12) + heightIn;
heightInMeters = totalInches * 0.0254;
}
// Calculate BMI
let bmi;
if (currentSystem === 'metric') {
bmi = weight / (heightInMeters * heightInMeters);
} else {
// For imperial: BMI = (weight in lbs * 703) / (height in inches)^2
const heightInInches = heightInMeters / 0.0254;
bmi = (weight * 703) / (heightInInches * heightInInches);
}
// Display results
displayResults(bmi, weight, heightInMeters);
}
// Display results
function displayResults(bmi, weight, heightInMeters) {
const resultsCard = document.getElementById('results-card');
// Determine category
const category = getBMICategory(bmi);
// Calculate ideal weight range
const idealWeight = calculateIdealWeight(heightInMeters);
// Get health advice
const advice = getHealthAdvice(category.key);
// Build results HTML
const resultsHTML = `
<div class="bmi-result">
<div class="bmi-value">${bmi.toFixed(1)}</div>
<div class="bmi-category category-${category.key}">${category.name}</div>
<div class="bmi-range">${category.range}</div>
<div class="bmi-details">
<div class="detail-row">
<span class="detail-label">Your Weight</span>
<span class="detail-value">${formatWeight(weight)}</span>
</div>
<div class="detail-row">
<span class="detail-label">Your Height</span>
<span class="detail-value">${formatHeight(heightInMeters)}</span>
</div>
<div class="detail-row">
<span class="detail-label">Healthy BMI Range</span>
<span class="detail-value">18.5 - 24.9</span>
</div>
<div class="detail-row">
<span class="detail-label">Healthy Weight Range</span>
<span class="detail-value">${idealWeight}</span>
</div>
</div>
<div class="health-advice ${category.key}">
<h3>${advice.title}</h3>
<p>${advice.message}</p>
</div>
</div>
`;
resultsCard.innerHTML = resultsHTML;
}
// Get BMI category
function getBMICategory(bmi) {
if (bmi < 18.5) {
return { key: 'underweight', name: 'Underweight', range: 'BMI less than 18.5' };
} else if (bmi >= 18.5 && bmi < 25) {
return { key: 'normal', name: 'Normal Weight', range: 'BMI 18.5 - 24.9' };
} else if (bmi >= 25 && bmi < 30) {
return { key: 'overweight', name: 'Overweight', range: 'BMI 25 - 29.9' };
} else {
return { key: 'obese', name: 'Obese', range: 'BMI 30 or greater' };
}
}
// Calculate ideal weight range
function calculateIdealWeight(heightInMeters) {
const minWeight = 18.5 * heightInMeters * heightInMeters;
const maxWeight = 24.9 * heightInMeters * heightInMeters;
if (currentSystem === 'metric') {
return `${minWeight.toFixed(1)} - ${maxWeight.toFixed(1)} kg`;
} else {
const minLbs = minWeight * 2.20462;
const maxLbs = maxWeight * 2.20462;
return `${minLbs.toFixed(1)} - ${maxLbs.toFixed(1)} lbs`;
}
}
// Get health advice
function getHealthAdvice(category) {
const advice = {
underweight: {
title: '💡 Health Advice',
message: 'Being underweight may indicate that you\'re not eating enough or you may be ill. If you\'re underweight, consult a doctor or dietitian for advice.'
},
normal: {
title: '✅ Excellent!',
message: 'You\'re in the healthy weight range! Maintain your weight through a balanced diet and regular physical activity.'
},
overweight: {
title: '⚠️ Health Alert',
message: 'Being overweight increases your risk of heart disease, diabetes, and other conditions. Consider adopting healthier eating habits and increasing physical activity.'
},
obese: {
title: '🚨 Important Notice',
message: 'Obesity significantly increases your risk of serious health conditions. We strongly recommend consulting a healthcare professional for personalized advice and support.'
}
};
return advice[category];
}
// Format weight for display
function formatWeight(weight) {
if (currentSystem === 'metric') {
return `${weight.toFixed(1)} kg`;
} else {
return `${weight.toFixed(1)} lbs`;
}
}
// Format height for display
function formatHeight(heightInMeters) {
if (currentSystem === 'metric') {
const heightCm = heightInMeters * 100;
return `${heightCm.toFixed(1)} cm`;
} else {
const totalInches = heightInMeters / 0.0254;
const feet = Math.floor(totalInches / 12);
const inches = Math.round(totalInches % 12);
return `${feet}' ${inches}"`;
}
}
// Show error message
function showError(message) {
const errorElement = document.getElementById('error-message');
errorElement.textContent = message;
errorElement.classList.add('show');
}
// Hide error message
function hideError() {
const errorElement = document.getElementById('error-message');
errorElement.classList.remove('show');
}
// Enter key support
document.querySelectorAll('input').forEach(input => {
input.addEventListener('keypress', function(event) {
if (event.key === 'Enter') {
calculateBMI();
}
});
});
// Clear inputs on page load
window.addEventListener('load', function() {
document.getElementById('weight').value = '';
document.getElementById('height-cm').value = '';
document.getElementById('height-ft').value = '';
document.getElementById('height-in').value = '';
});