-
Notifications
You must be signed in to change notification settings - Fork 0
/
body.js
37 lines (31 loc) · 1.26 KB
/
body.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
function calculateBodyFatPercentage() {
var gender = document.getElementById("gender").value;
var weight = parseFloat(document.getElementById("weight").value);
var waist = parseFloat(document.getElementById("waist").value);
var hip = parseFloat(document.getElementById("hip").value);
if (isNaN(weight) || isNaN(waist) || isNaN(hip)) {
alert("Please enter valid weight, waist circumference, and hip circumference.");
return;
}
var bodyFatPercentage = calculateBFPercentage(gender, weight, waist, hip);
document.getElementById("result").innerHTML = "Your body fat percentage is approximately " + bodyFatPercentage.toFixed(2) + "%.";
}
function calculateBFPercentage(gender, weight, waist, hip) {
var factorA, factorB, factorC;
if (gender === "male") {
factorA = 86.010;
factorB = 70.041;
factorC = 30;
} else if (gender === "female") {
factorA = 163.205;
factorB = 97.684;
factorC = 28;
} else {
alert("Invalid gender selection.");
return null;
}
var leanBodyMass = weight - (0.8 * (weight * (factorC / 100)));
var bodyFatWeight = weight - leanBodyMass;
var bodyFatPercentage = (bodyFatWeight / weight) * 100;
return bodyFatPercentage;
}