-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
51 lines (41 loc) · 1.55 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
document.addEventListener("DOMContentLoaded", function () {
const form = document.getElementById("calc-form");
const nameInput = document.getElementById("name");
const nameError = document.getElementById("name-error");
form.addEventListener("submit", function (event) {
event.preventDefault();
if (!nameInput.value) {
nameError.style.display = "block";
return;
} else {
nameError.style.display = "none";
}
const name = nameInput.value;
const height = parseFloat(document.getElementById("height").value);
const weight = parseFloat(document.getElementById("weight").value);
const bmi = weight / (height * height);
const resultElement = document.getElementById("result");
let category;
if (bmi < 18.5) {
category = "Underweight";
resultElement.style.backgroundColor = "#FFFF00";
} else if (bmi < 25) {
category = "Normal weight";
resultElement.style.backgroundColor = "#00FF00";
} else if (bmi < 30) {
category = "Overweight";
resultElement.style.backgroundColor = "#FFA500";
} else {
category = "Obesity";
resultElement.style.backgroundColor = "#FF0000";
}
resultElement.innerHTML = `<p> ${name}, your BMI is ${bmi.toFixed(
2
)}.</p><p>You are in the category: ${category}</p>`;
document.getElementById("category").value = category;
let formData = new FormData(form);
for (let [key, value] of formData.entries()) {
console.log(key + " : " + value);
}
});
});