-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
208 lines (154 loc) · 4.31 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>1RepMax calculator (vanilla JS)</title>
</head>
<body>
<style>
body {
font-family: Verdana, Arial, sans-serif;
}
small {
font-size: 50%;
}
label {
font-size: 120%;
}
input[type="number"] {
margin: 0.1em 0.5em;
width: 60px;
font-size:105%;
}
th, td {
padding: 5px 10px;
text-align: center;
font-size:150%;
}
@media(max-width: 600px) {
th, td {
padding: 1px 20px;
font-size: 130%;
}
}
</style>
<br>
<label># Reps
<input
id="numReps"
type="number"
min="1"
step="1"
placeholder="1"
autofocus
/>
</label>
<br>
<label>Weight
<input
id="weight"
type="number"
min="1"
step="0.1"
placeholder="100"
/>
</label>
<hr />
<label
for="maxReps"
id="maxRepsLabel"
data-text="Show ($) rows"
></label>
<input
id="maxReps"
type="number"
min="3"
max="100"
step="1"
placeholder="10"
/>
<hr />
<table>
<thead>
<tr>
<th>Reps</th>
<th>Weight</th>
</tr>
</thead>
<tbody id="outputTableBody"></tbody>
</table>
<script>
const WEIGHT_DISPLAY_PLACES = 1;
const WEIGHT_PERCENT_PLACES = 2;
const doc = document;
const qi = doc.getElementById.bind(doc);
const numRepsInput = qi("numReps");
const weightInput = qi("weight");
const maxRepsInput = qi("maxReps");
const oneRepMaxFromRepsAndWeight_Epley = (reps, weight) => {
return weight * ( 1 + ( reps > 1 ? reps / 30 : 0) );
};
const weightFromRepsAtOneRepMax_Epley = (reps, oneRepMax) => {
return oneRepMax / ( reps > 1 ? ( 1 + reps / 30 ) : 1 );
};
// const repsFromWeightAtOneRepMax_Epley = (weight, oneRepMax) => {
// return ( oneRepMax / weight - 1 ) * 30;
// };
const oneRepMaxFromRepsAndWeight = (reps, weight) => { // calc 1RM based on provided weight and # of reps
return oneRepMaxFromRepsAndWeight_Epley(reps, weight);
};
const weightFromRepsAtOneRepMax = (reps, oneRepMax) => { // calc weight for a provided # of reps based on 1RM
return weightFromRepsAtOneRepMax_Epley(reps, oneRepMax);
};
// const repsFromWeightAtOneRepMax = (weight, oneRepMax) => { // calc # of reps for a provided weight based on 1RM
// return repsFromWeightAtOneRepMax_Epley(weight, oneRepMax);
// };
const getHtmlWeightFromRepsAndWeight = (reps, weight1RM) => {
const weight = weightFromRepsAtOneRepMax(reps, weight1RM);
const percentOf1RM = Math.round( ( weight / weight1RM ) * 100, WEIGHT_PERCENT_PLACES );
return `${ weight.toFixed(WEIGHT_DISPLAY_PLACES) } <small>(${ percentOf1RM }%)</small>`;
};
const renderTableBodyRows = (tableBody, numReps, weight, maxReps) => {
tableBody.innerHTML = "";
const weight1RM = oneRepMaxFromRepsAndWeight(numReps, weight);
for (let reps = 1; reps <= maxReps; reps ++) {
const cellReps = Object.assign( doc.createElement("td"), { textContent: reps } );
const cellWeight = Object.assign( doc.createElement("td"), { innerHTML: getHtmlWeightFromRepsAndWeight( reps, weight1RM ) } );
const row = doc.createElement("tr");
row.appendChild(cellReps);
row.appendChild(cellWeight);
tableBody.appendChild(row);
};
};
const parseInputValues = changedElementId => {
const numReps = numRepsInput.valueAsNumber >= 1 ? numRepsInput.valueAsNumber : 1;
const weight = weightInput.valueAsNumber >= 1 ? weightInput.valueAsNumber : 100;
const maxRepsRaw = maxRepsInput.valueAsNumber >= maxRepsInput.min ? maxRepsInput.valueAsNumber : maxRepsInput.placeholder;
const maxReps = Math.min( maxRepsRaw, maxRepsInput.max ) | 0;
qi("maxRepsLabel").innerText = maxRepsLabel.dataset.text.replace("$", maxReps); // data-text="Show ($) rows"
const validMax = (
isNaN(maxRepsInput.value)
|| maxRepsInput.value >= maxRepsInput.min
|| false
);
if(validMax ||
[ "numReps", "weight" ].includes(changedElementId)
) {
maxRepsInput.value = maxReps;
};
return [numReps, weight, maxReps];
};
const update = evt => {
const changedElementId = evt?.target.id;
const [numReps, weight, maxReps] = parseInputValues(changedElementId);
const outputTableBody = qi("outputTableBody");
renderTableBodyRows( outputTableBody, numReps, weight, maxReps);
};
const init = () => {
[ numRepsInput, weightInput, maxRepsInput]
.forEach( el => el.addEventListener("input", update) );
update();
};
doc.addEventListener("DOMContentLoaded", init);
</script>
</body>
</html>