-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
106 lines (87 loc) · 3.39 KB
/
script.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
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
// Get a reference to the result container
const resultCont = document.getElementById("resultCont");
const result = document.createElement("h1");
// Get input fields
const inputFields = document.querySelectorAll("input");
// Function to calculate BMI
const calculate = () => {
// Get user input values
const gender = document.getElementById("gender").value;
const age = document.getElementById("age").value;
const heightFeet = parseFloat(document.getElementById("heightFeet").value);
const heightInch = parseFloat(document.getElementById("heightInch").value);
const weight = parseFloat(document.getElementById("weight").value);
// Calculate height in meters
let heightInMeters = (heightFeet * 12 + heightInch) * 0.0254;
// Clear previous result if it exists
const prevResult = document.getElementById("result");
if (prevResult) {
resultCont.removeChild(prevResult);
}
// Function to display and style the BMI result
const displayResult = () => {
result.id = "result";
result.style.fontWeight = "bold";
const bmi = weight / (heightInMeters * heightInMeters);
result.innerHTML = bmi.toFixed(2);
if (bmi < 18.5) {
result.style.color = "#FEB132"; // Underweight color
} else if (bmi >= 18.5 && bmi <= 24.9) {
result.style.color = "#30A232"; // Normal weight color
} else if (bmi >= 25 && bmi <= 30) {
result.style.color = "#E96024"; // Overweight color
} else {
result.style.color = "#C0101B"; // Obese color
}
// Append the result to the result container
resultCont.appendChild(result);
};
// Check for invalid input
if (
Number.isNaN(age) ||
age <= 0 ||
isNaN(heightFeet) ||
isNaN(heightInch) ||
isNaN(weight)
) {
alert("Invalid Input");
} else {
// Clear input fields
inputFields.forEach((input) => {
input.value = "";
});
// Clear previous user information
let userInput = document.querySelectorAll("#resultCont p");
userInput.forEach((p) => {
resultCont.removeChild(p);
});
// Display user information
const genderCont = document.createElement("p");
genderCont.innerHTML = "Gender: " + gender;
resultCont.appendChild(genderCont);
const ageCont = document.createElement("p");
ageCont.innerHTML = "Age: " + age;
resultCont.appendChild(ageCont);
const heightCont = document.createElement("p");
heightCont.innerHTML =
"Height: " + heightFeet.toString() + "'" + heightInch.toString();
resultCont.appendChild(heightCont);
const weightCont = document.createElement("p");
weightCont.innerHTML = "Weight: " + weight;
resultCont.appendChild(weightCont);
// Calculate and display the BMI result
displayResult();
}
};
// Function to reset input and resultont fields
const reset = () => {
const inputField = inputFields
inputField.forEach((input) => {
input.value = "";
});
const userInput = document.querySelectorAll("#resultCont p");
userInput.forEach((p) => {
resultCont.removeChild(p);
});
resultCont.removeChild(result);
};