-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBMI.js
64 lines (56 loc) · 2.43 KB
/
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
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
// Select DOM elements
const form = document.querySelector("form");
const heightInput = document.querySelector("#height");
const weightInput = document.querySelector("#weight");
const resultDiv = document.querySelector(".result");
const detailsDiv = document.querySelector(".details");
// Event Listener for Form Submission
form.addEventListener("submit", (e) => {
e.preventDefault(); // Prevent form refresh
// Get user input values
const height = parseFloat(heightInput.value) / 100; // Convert cm to meters
const weight = parseFloat(weightInput.value);
// Validate inputs
if (isNaN(height) || isNaN(weight) || height <= 0 || weight <= 0) {
resultDiv.textContent = "Please enter valid values for height and weight.";
resultDiv.style.color = "red";
detailsDiv.textContent = ""; // Clear previous details
return;
}
// Calculate BMI
const bmi = (weight / (height * height)).toFixed(1);
// Determine BMI category
let category = "";
if (bmi < 18.5) {
category = "Underweight";
resultDiv.style.color = "#007bff";
} else if (bmi >= 18.5 && bmi < 24.9) {
category = "Normal weight";
resultDiv.style.color = "green";
} else if (bmi >= 25 && bmi < 29.9) {
category = "Overweight";
resultDiv.style.color = "orange";
} else {
category = "Obese";
resultDiv.style.color = "red";
}
// Display BMI and category
resultDiv.textContent = `Your BMI is ${bmi} (${category})`;
// Provide category details
detailsDiv.textContent = getBMIDetails(category);
});
// Function to return details about BMI categories
function getBMIDetails(category) {
switch (category) {
case "Underweight":
return "Being underweight can be associated with health risks such as weakened immunity and nutritional deficiencies. Consider consulting a healthcare provider for guidance.";
case "Normal weight":
return "You have a healthy weight. Maintain your lifestyle to continue enjoying good health.";
case "Overweight":
return "Being overweight may lead to health issues like diabetes, hypertension, or heart diseases. A balanced diet and regular exercise can help.";
case "Obese":
return "Obesity increases the risk of chronic illnesses. It is advisable to seek professional help to manage your weight effectively.";
default:
return "";
}
}