-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculators.js
More file actions
210 lines (177 loc) · 8.36 KB
/
Copy pathcalculators.js
File metadata and controls
210 lines (177 loc) · 8.36 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Shared form validation function
function validatePositiveNumber(value, fieldName) {
if (isNaN(value) || value <= 0) {
return `${fieldName} must be a positive number. `;
}
return '';
}
document.addEventListener('DOMContentLoaded', function() {
// 1RM Calculator
const oneRMForm = document.getElementById('oneRMForm');
if (oneRMForm) {
oneRMForm.addEventListener('submit', function(e) {
e.preventDefault();
const weight = parseFloat(document.getElementById('weight').value);
const reps = parseInt(document.getElementById('reps').value);
const resultDiv = document.getElementById('oneRMResult');
const errorDiv = document.getElementById('oneRMError');
// Validate inputs
let errorMessage = '';
errorMessage += validatePositiveNumber(weight, 'Weight');
errorMessage += validatePositiveNumber(reps, 'Reps');
if (errorMessage) {
errorDiv.textContent = errorMessage;
resultDiv.classList.add('d-none');
errorDiv.classList.remove('d-none');
return;
}
// Formula to calculate 1RM: weight * (1 + (reps/30))
const epley = Math.round(weight * (1 + (reps/30)));
// Brzycki formula: weight * 36 / (37 - reps)
const brzycki = Math.round(weight * 36 / (37 - reps));
// Lander formula: (100 * weight) / (101.3 - 2.67123 * reps)
const lander = Math.round((100 * weight) / (101.3 - 2.67123 * reps));
// Update the result div
document.getElementById('epley').textContent = epley;
document.getElementById('brzycki').textContent = brzycki;
document.getElementById('lander').textContent = lander;
errorDiv.classList.add('d-none');
resultDiv.classList.remove('d-none');
});
}
// BMI Calculator
const bmiForm = document.getElementById('bmiForm');
if (bmiForm) {
bmiForm.addEventListener('submit', function(e) {
e.preventDefault();
const height = parseFloat(document.getElementById('height').value);
const weight = parseFloat(document.getElementById('weight').value);
const unit = document.getElementById('unit').value;
const resultDiv = document.getElementById('bmiResult');
const errorDiv = document.getElementById('bmiError');
// Validate inputs
let errorMessage = '';
errorMessage += validatePositiveNumber(height, 'Height');
errorMessage += validatePositiveNumber(weight, 'Weight');
if (errorMessage) {
errorDiv.textContent = errorMessage;
resultDiv.classList.add('d-none');
errorDiv.classList.remove('d-none');
return;
}
// Calculate BMI based on the selected unit
let bmi;
if (unit === 'metric') {
// BMI = weight(kg) / height(m)^2
bmi = weight / ((height/100) * (height/100));
} else {
// BMI = 703 * weight(lb) / height(in)^2
bmi = 703 * weight / (height * height);
}
bmi = Math.round(bmi * 10) / 10; // Round to 1 decimal place
// Determine BMI category
let category;
if (bmi < 18.5) {
category = 'Underweight';
} else if (bmi >= 18.5 && bmi < 25) {
category = 'Normal weight';
} else if (bmi >= 25 && bmi < 30) {
category = 'Overweight';
} else {
category = 'Obesity';
}
// Update result
document.getElementById('bmiValue').textContent = bmi;
document.getElementById('bmiCategory').textContent = category;
errorDiv.classList.add('d-none');
resultDiv.classList.remove('d-none');
});
}
// BMR Calculator
const bmrForm = document.getElementById('bmrForm');
if (bmrForm) {
bmrForm.addEventListener('submit', function(e) {
e.preventDefault();
const age = parseInt(document.getElementById('age').value);
const gender = document.querySelector('input[name="gender"]:checked').value;
const height = parseFloat(document.getElementById('height').value);
const weight = parseFloat(document.getElementById('weight').value);
const unit = document.getElementById('unit').value;
const activity = document.getElementById('activity').value;
const formula = document.getElementById('formula').value;
const resultDiv = document.getElementById('bmrResult');
const errorDiv = document.getElementById('bmrError');
// Validate inputs
let errorMessage = '';
errorMessage += validatePositiveNumber(age, 'Age');
errorMessage += validatePositiveNumber(height, 'Height');
errorMessage += validatePositiveNumber(weight, 'Weight');
if (errorMessage) {
errorDiv.textContent = errorMessage;
resultDiv.classList.add('d-none');
errorDiv.classList.remove('d-none');
return;
}
// Convert to metric if using imperial
let weightKg, heightCm;
if (unit === 'metric') {
weightKg = weight;
heightCm = height;
} else {
weightKg = weight * 0.453592; // lb to kg
heightCm = height * 2.54; // in to cm
}
// Calculate BMR using selected formula
let bmr;
if (formula === 'mifflin') {
// Mifflin-St Jeor Equation:
if (gender === 'male') {
bmr = (10 * weightKg) + (6.25 * heightCm) - (5 * age) + 5;
} else {
bmr = (10 * weightKg) + (6.25 * heightCm) - (5 * age) - 161;
}
} else {
// Harris-Benedict Equation:
if (gender === 'male') {
bmr = 88.362 + (13.397 * weightKg) + (4.799 * heightCm) - (5.677 * age);
} else {
bmr = 447.593 + (9.247 * weightKg) + (3.098 * heightCm) - (4.330 * age);
}
}
// Calculate TDEE (Total Daily Energy Expenditure)
const activityFactors = {
'sedentary': 1.2,
'light': 1.375,
'moderate': 1.55,
'active': 1.725,
'very-active': 1.9
};
const tdee = bmr * activityFactors[activity];
// Round results
bmr = Math.round(bmr);
const tdeeCal = Math.round(tdee);
// Update results
document.getElementById('bmrValue').textContent = bmr;
document.getElementById('tdeeValue').textContent = tdeeCal;
errorDiv.classList.add('d-none');
resultDiv.classList.remove('d-none');
});
}
// Unit conversion functionality
const unitSelects = document.querySelectorAll('.unit-select');
unitSelects.forEach(select => {
select.addEventListener('change', function() {
const unit = this.value;
const form = this.closest('form');
const weightLabel = form.querySelector('.weight-label');
const heightLabel = form.querySelector('.height-label');
if (unit === 'metric') {
weightLabel.textContent = 'Weight (kg)';
heightLabel.textContent = 'Height (cm)';
} else {
weightLabel.textContent = 'Weight (lb)';
heightLabel.textContent = 'Height (inches)';
}
});
});
});