-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbmi.js
33 lines (27 loc) · 1013 Bytes
/
bmi.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
function calculateBMI() {
var weightInput = document.getElementById("weight");
var heightInput = document.getElementById("height");
var resultDiv = document.getElementById("result");
var weight = parseFloat(weightInput.value);
var height = parseFloat(heightInput.value) / 100; // Convert height to meters
// Validate inputs
if (isNaN(weight) || isNaN(height)) {
resultDiv.textContent = "Please enter valid weight and height.";
return;
}
// Calculate BMI
var bmi = weight / (height * height);
// Determine BMI category
var category;
if (bmi < 18.5) {
category = "Underweight";
} else if (bmi >= 18.5 && bmi < 25) {
category = "Normal weight";
} else if (bmi >= 25 && bmi < 30) {
category = "Overweight";
} else {
category = "Obese";
}
// Display the result
resultDiv.textContent = "Your BMI is: " + bmi.toFixed(2) + " (" + category + ")";
}