-
Notifications
You must be signed in to change notification settings - Fork 903
/
Copy pathindex.js
67 lines (63 loc) · 2.51 KB
/
index.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
const form = document.querySelector("form");
const units = document.querySelector("#units");
const heightLabel = document.querySelector("#height-label");
const weightLabel = document.querySelector("#weight-label");
const height = document.querySelector("#height");
const weight = document.querySelector("#weight");
form.addEventListener("submit", function (e) {
e.preventDefault();
const formData = new FormData(e.target);
const inputValues = Object.fromEntries(formData);
let height = +inputValues.height;
let weight = +inputValues.weight;
const results = document.querySelector("#results");
const bmiresult = document.querySelector("#bmiresult");
if (units.value === "Imperial") {
height = height * 2.54;
weight = weight * 0.453592;
console.log(units.value);
}
if (height === "" || height < 0 || isNaN(height)) {
//NaN !== NaN
results.innerHTML = "Please provide a valid height";
} else if (weight === "" || weight < 0 || isNaN(weight)) {
results.innerHTML = "Please provide a valid weight";
} else {
//calculate BMI
const bmi = (weight / ((height * height) / 10000)).toFixed(2);
//display the results
results.innerHTML = `<span>${bmi}</span>`;
if (bmi <= 16) bmiresult.innerHTML = "<h2>Severe Thinness</h2>";
else if (bmi > 16 && bmi <= 17)
bmiresult.innerHTML = "<h2>Moderate Thinness</h2>";
else if (bmi > 17 && bmi <= 18.5)
bmiresult.innerHTML = "<h2>Mild Thinness</h2>";
else if (bmi > 18.5 && bmi <= 25) bmiresult.innerHTML = "<h2>Normal</h2>";
else if (bmi > 25 && bmi <= 30) bmiresult.innerHTML = "<h2>Overweight</h2>";
else if (bmi > 30 && bmi <= 35)
bmiresult.innerHTML = "<h2>Obese Class I</h2>";
else if (bmi > 35 && bmi <= 40)
bmiresult.innerHTML = "<h2>Obese Class II</h2>";
else bmiresult.innerHTML = "<h2>Obese Class III</h2>";
}
});
// Allow user to change between metric and imperial.
units.addEventListener("change", (e) => {
if (e.target.value === "Imperial") {
height.placeholder = "Height in IN";
weight.placeholder = "Weight in LB";
if (height.value) {
// convert current input to imperial
height.value = (+height.value / 2.54).toFixed(2);
weight.value = (+weight.value / 0.453592).toFixed(2);
}
} else {
height.placeholder = "Height in CM";
weight.placeholder = "Weight in KG";
if (height.value) {
// convert current input to metric
height.value = (+height.value * 2.54).toFixed(2);
weight.value = (+weight.value * 0.453592).toFixed(2);
}
}
});