-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbmi.js
63 lines (61 loc) · 1.9 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
var age=document.getElementById("age");
var height=document.getElementById("height");
var weight=document.getElementById("weight");
var male=document.getElementById("m");
var female=document.getElementById("f");
var form=document.getElementById("form");
function validateForm() {
if(age.value == ''|| height.value == ''|| weight.value == ''||(male.checked==false && female.checked==false)) {
alert("All fields are required");
document.getElementById("submit").removeEventListener("click",countBmi);
}
else {
countBmi();
}
}
document.getElementById("submit").addEventListener("click",validateForm);
function countBmi() {
var p = [age.value,height.value,weight.value];
if(male.checked) {
p.push("male");
}
else if(female.checked) {
p.push("female");
}
form.reset();
var bmi = Number(p[2])/(Number(p[1])/100*Number(p[1])/100);
var result = '';
if(bmi<18.5)
{
result = 'Underweight';
}
else if(18.5<=bmi &&bmi<=24.9)
{
result='Healthy';
}
else if(25<=bmi && bmi <=29.9)
{
result='Overweight';
}
else if(30<=bmi&&bmi<=34.9)
{
result='Obese';
}
else if(35<=bmi)
{
result='Extremely obese';
}
var h1 = document.createElement("h1");
var h2 = document.createElement("h2");
var t = document.createTextNode(result);
var b = document.createTextNode("BMI: ");
var r = document.createTextNode(parseFloat(bmi).toFixed(2));
h1.appendChild(t);
h2.appendChild(b);
h2.appendChild(r);
document.body.appendChild(h1);
document.body.appendChild(h2);
document.getElementById("submit").removeEventListener("click",countBmi);
document.getElementById("submit").removeEventListener("click",validateForm);
}
document.getElementById("submit").addEventListener("click",countBmi);