-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
68 lines (56 loc) · 2.59 KB
/
scripts.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
alert("An app developed independently by @simplyYan (Wesley Yan Soares Brehmer), under a CC0-1.0 license.")
document.getElementById('userForm').addEventListener('submit', function(e) {
e.preventDefault();
const name = document.getElementById('name').value;
const age = document.getElementById('age').value;
const height = document.getElementById('height').value;
const weight = document.getElementById('weight').value;
const activityLevel = document.getElementById('activityLevel').value;
var result = calculateIdealWeight(height, weight);
const imc = result.bmi;
const idealWeightRange = result.idealWeight;
const leanMass = calculateLeanMass(weight, imc);
const fatMass = calculateFatMass(weight, leanMass);
const calories = calculateCalories(weight, height, age, activityLevel);
const waterIntake = calculateWaterIntake(weight);
displayResults(name, imc, idealWeightRange, calories, waterIntake, leanMass, fatMass);
});
function calculateIMC(weight, height) {
const heightInMeters = height / 100;
return (weight / (heightInMeters * heightInMeters)).toFixed(2);
}
function calculateIdealWeight(height, weight) {
height = height / 100;
var bmi = weight / Math.pow(height, 2);
var minIdealWeight = 18.5 * Math.pow(height, 2);
var maxIdealWeight = 24.9 * Math.pow(height, 2);
return {
bmi: bmi.toFixed(2),
idealWeight: [minIdealWeight.toFixed(2), maxIdealWeight.toFixed(2)]
};
}
function calculateCalories(weight, height, age, activityLevel) {
const TMB = 88.36 + (13.4 * weight) + (4.8 * height) - (5.7 * age);
return (TMB * activityLevel).toFixed(2);
}
function calculateWaterIntake(weight) {
return (weight * 0.035).toFixed(2);
}
function calculateLeanMass(weight, imc) {
return (weight * (1 - (imc / 100))).toFixed(2);
}
function calculateFatMass(weight, leanMass) {
return (weight - leanMass).toFixed(2);
}
function displayResults(name, imc, idealWeightRange, calories, waterIntake, leanMass, fatMass) {
const resultsDiv = document.getElementById('results');
resultsDiv.innerHTML = `
<h2>Results for ${name}</h2>
<p><strong>BMI:</strong> ${imc}</p>
<p><strong>Ideal weight:</strong> ${idealWeightRange[0]} kg - ${idealWeightRange[1]} kg</p>
<p><strong>Daily Calories:</strong> ${calories} kcal</p>
<p><strong>Daily Water Intake:</strong> ${waterIntake} liters</p>
<p><strong>Lean Mass:</strong> ${leanMass} kg</p>
<p><strong>Fat Mass:</strong> ${fatMass} kg</p>
`;
}