-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
44 lines (36 loc) · 1.38 KB
/
script.js
File metadata and controls
44 lines (36 loc) · 1.38 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
const bmiText = document.getElementById("bmi");
const descText = document.getElementById("desc");
const form = document.querySelector("form");
form.addEventListener("submit", onFormSubmit);
form.addEventListener("reset", onFormReset);
function onFormReset() {
bmiText.textContent = 0;
bmiText.className = "";
descText.textContent = "N/A";
}
function onFormSubmit(e) {
e.preventDefault();
const weight = parseFloat(form.weight.value);
const height = parseFloat(form.height.value);
if (isNaN(weight) || isNaN(height) || weight <= 0 || height <= 0) {
alert("Please enter a valid weight and height");
return;
}
const heightInMeters = height / 100; // cm -> m
const bmi = weight / Math.pow(heightInMeters, 2);
const desc = interpretBMI(bmi);
bmiText.textContent = bmi.toFixed(2);
bmiText.className = desc;
descText.innerHTML = `You are <strong>${desc}</strong>`;
}
function interpretBMI(bmi) {
if (bmi < 18.5) {
return " underweight, You Should take bulking food and maintain a Calorie Surplus Diet";
} else if (bmi < 25) {
return " healthy, You Should Eat a balance diet and focus on strength tranning";
} else if (bmi < 30) {
return " overweight,You Should always maintain a calorie defiect Diet and focus on both cardio and resistance tranning";
} else {
return " You are obese,You should stictly follow a strict calorie defiect diet plan";
}
}