-
Notifications
You must be signed in to change notification settings - Fork 966
Expand file tree
/
Copy pathscript.js
More file actions
232 lines (201 loc) · 6.94 KB
/
script.js
File metadata and controls
232 lines (201 loc) · 6.94 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
const form = document.querySelector('#bmi-form');
const unitButtons = document.querySelectorAll('.unit-btn');
const heightLabel = document.querySelector('#height-label');
const weightLabel = document.querySelector('#weight-label');
const heightInput = document.querySelector('#height');
const weightInput = document.querySelector('#weight');
const heightUnit = document.querySelector('#height-unit');
const weightUnit = document.querySelector('#weight-unit');
const resultsDiv = document.querySelector('#results');
const bmiNumber = document.querySelector('#bmi-number');
const categoryText = document.querySelector('#category-text');
const bmiRange = document.querySelector('#bmi-range');
let currentUnit = 'metric';
// Unit toggle functionality
unitButtons.forEach(btn => {
btn.addEventListener('click', function() {
// Remove active class from all buttons
unitButtons.forEach(b => b.classList.remove('active'));
// Add active class to clicked button
this.classList.add('active');
currentUnit = this.dataset.unit;
updateLabels();
// Convert values if they exist
if (heightInput.value || weightInput.value) {
convertValues();
}
});
});
function updateLabels() {
if (currentUnit === 'imperial') {
heightLabel.textContent = 'Height (in)';
weightLabel.textContent = 'Weight (lb)';
heightUnit.textContent = 'in';
weightUnit.textContent = 'lb';
} else {
heightLabel.textContent = 'Height (cm)';
weightLabel.textContent = 'Weight (kg)';
heightUnit.textContent = 'cm';
weightUnit.textContent = 'kg';
}
}
function convertValues() {
if (currentUnit === 'imperial') {
// Convert from metric to imperial
if (heightInput.value) {
heightInput.value = (parseFloat(heightInput.value) / 2.54).toFixed(2);
}
if (weightInput.value) {
weightInput.value = (parseFloat(weightInput.value) / 0.453592).toFixed(2);
}
} else {
// Convert from imperial to metric
if (heightInput.value) {
heightInput.value = (parseFloat(heightInput.value) * 2.54).toFixed(2);
}
if (weightInput.value) {
weightInput.value = (parseFloat(weightInput.value) * 0.453592).toFixed(2);
}
}
}
function getBMICategory(bmi) {
if (bmi < 18.5) {
return {
category: 'underweight',
text: 'Underweight',
range: 'Below 18.5'
};
} else if (bmi >= 18.5 && bmi < 25) {
return {
category: 'normal',
text: 'Normal Weight',
range: '18.5 - 24.9'
};
} else if (bmi >= 25 && bmi < 30) {
return {
category: 'overweight',
text: 'Overweight',
range: '25 - 29.9'
};
} else {
return {
category: 'obese',
text: 'Obese',
range: '30 and above'
};
}
}
function displayResults(bmi) {
const categoryInfo = getBMICategory(bmi);
// Remove all category classes
resultsDiv.classList.remove('underweight', 'normal', 'overweight', 'obese');
// Add the appropriate category class
resultsDiv.classList.add(categoryInfo.category);
// Animate BMI number
animateValue(bmiNumber, 0, bmi, 1000);
// Update category text and range
categoryText.textContent = categoryInfo.text;
bmiRange.textContent = `BMI Range: ${categoryInfo.range}`;
// Show results
resultsDiv.classList.remove('hidden');
// Scroll to results smoothly
resultsDiv.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
}
function animateValue(element, start, end, duration) {
let startTimestamp = null;
const step = (timestamp) => {
if (!startTimestamp) startTimestamp = timestamp;
const progress = Math.min((timestamp - startTimestamp) / duration, 1);
const current = progress * (end - start) + start;
element.textContent = current.toFixed(1);
if (progress < 1) {
window.requestAnimationFrame(step);
} else {
element.textContent = end.toFixed(1);
}
};
window.requestAnimationFrame(step);
}
form.addEventListener('submit', function(e) {
e.preventDefault();
let height = parseFloat(heightInput.value);
let weight = parseFloat(weightInput.value);
// Validation
if (!height || height <= 0 || isNaN(height)) {
showError('Please enter a valid height');
heightInput.focus();
return;
}
if (!weight || weight <= 0 || isNaN(weight)) {
showError('Please enter a valid weight');
weightInput.focus();
return;
}
// Convert to metric if imperial
if (currentUnit === 'imperial') {
height = height * 2.54; // inches to cm
weight = weight * 0.453592; // pounds to kg
}
// Validate converted values
if (height < 50 || height > 300) {
showError('Height should be between 50cm and 300cm (or equivalent)');
return;
}
if (weight < 10 || weight > 500) {
showError('Weight should be between 10kg and 500kg (or equivalent)');
return;
}
// Calculate BMI: weight (kg) / height (m)²
// Height is in cm, so we divide by 100 to get meters
const heightInMeters = height / 100;
const bmi = weight / (heightInMeters * heightInMeters);
// Display results
displayResults(bmi);
});
function showError(message) {
// Remove any existing error styling
heightInput.style.borderColor = '';
weightInput.style.borderColor = '';
// Show error message (you could add a toast notification here)
alert(message);
// Add error styling to inputs
if (!heightInput.value || heightInput.value <= 0) {
heightInput.style.borderColor = '#ff6b6b';
}
if (!weightInput.value || weightInput.value <= 0) {
weightInput.style.borderColor = '#ff6b6b';
}
// Remove error styling after 3 seconds
setTimeout(() => {
heightInput.style.borderColor = '';
weightInput.style.borderColor = '';
}, 3000);
}
// Add input validation on blur
heightInput.addEventListener('blur', function() {
const value = parseFloat(this.value);
if (this.value && (value <= 0 || isNaN(value))) {
this.style.borderColor = '#ff6b6b';
} else {
this.style.borderColor = '';
}
});
weightInput.addEventListener('blur', function() {
const value = parseFloat(this.value);
if (this.value && (value <= 0 || isNaN(value))) {
this.style.borderColor = '#ff6b6b';
} else {
this.style.borderColor = '';
}
});
// Allow Enter key to submit
heightInput.addEventListener('keypress', function(e) {
if (e.key === 'Enter') {
weightInput.focus();
}
});
weightInput.addEventListener('keypress', function(e) {
if (e.key === 'Enter') {
form.dispatchEvent(new Event('submit'));
}
});