-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
138 lines (111 loc) · 4.24 KB
/
app.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
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
$(function () {
const buildZone = function (name, lowHr, highHr) {
return {
name: name,
lowHr: lowHr,
highHr: highHr,
}
};
const drawZone = function (container, zone) {
const row = $("<tr/>")
.append($("<td/>", {text: zone.name}))
.append($("<td/>", {text: zone.lowHr}))
.append($("<td/>", {text: zone.highHr > 0 ? zone.highHr : "Max"}));
$(container).append(row);
};
const calcZones = function (aet, ant, hrmax) {
return [
buildZone("Recovery", 0, calcPercentage(aet, 80)),
buildZone("Z1", calcPercentage(aet, 80) + 1, calcPercentage(aet, 90)),
buildZone("Z2", calcPercentage(aet, 90) + 1, calcPercentage(aet, 100)),
buildZone("Z3", calcPercentage(aet, 100) + 1, calcPercentage(ant, 100)),
buildZone("Z4", calcPercentage(ant, 100) + 1, calcPercentage(hrmax, 100)),
]
};
const calcPercentage = function (num, percent) {
return Math.floor((percent / 100) * num);
};
const calcDiffPercent = function (n1, n2) {
return Math.round(((n2 - n1) / n2) * 100);
};
// Form stuff
const resultsContainer = $("#results");
const handleInput = function (aet, ant, hrmax) {
trackSubmit(aet, ant, hrmax);
const zonesContainer = $("<table/>", {
"id": "zones",
"class": "table table-striped",
});
resultsContainer.empty();
resultsContainer.append(zonesContainer);
const zones = calcZones(aet, ant, hrmax);
zones.forEach(function (zone) {
drawZone(zonesContainer, zone);
});
const aetDiff = calcDiffPercent(aet, ant);
const isAerobicDeficient = aetDiff > 10.0;
const adsMsg = isAerobicDeficient ?
`Your current AnT/AeT diff is ${aetDiff}% suggesting that you might suffer from ADS. You can gain from mainly training in Z2 (just under AeT) until this value is below 10%.` :
`Your current AnT/AeT diff is ${aetDiff}%. You can benefit from higher intensity workouts.`;
const alertClass = isAerobicDeficient ? "alert-warning" : "alert-success";
const alertDiv = $("<div/>", {
"class": "alert " + alertClass,
"role": "alert",
});
alertDiv.append($("<h4/>", {"class": "alert-heading"})
.append($("<a/>", {
"href": "https://www.uphillathlete.com/diy-anaerobic-test/",
"target": "_blank",
"text": "The 10 Percent Test"
})));
alertDiv.append($("<p/>", {
"class": "mb-0",
"text": adsMsg,
}));
if (isAerobicDeficient) {
alertDiv.append($("<a/>", {
"href": "https://www.uphillathlete.com/aerobic-deficiency-syndrome/",
"target": "_blank",
"text": "Aerobic Deficiency Syndrome (ADS)",
}));
}
resultsContainer.append(alertDiv);
};
const configureValidation = function () {
const aet = $("#aet");
const ant = $("#ant");
const hrmax = $("#hrmax");
// Allow 0 to be used if max HR is unknown
ant.attr("max", hrmax.val() > 0 ? hrmax.val() : null);
aet.attr("max", ant.val());
};
$('#uphill-form input').on("keyup", function (e) {
configureValidation()
});
$('#uphill-form').on('submit', function (e) {
e.preventDefault();
configureValidation();
const aet = $("#aet").val();
const ant = $("#ant").val();
const hrmax = $("#hrmax").val();
if (aet > 0 && ant > 0) {
handleInput(aet, ant, hrmax);
}
return false;
});
const isAnalyticsAvailable = function() {
return typeof ga === 'function';
}
const initAnalytics = function() {
if (isAnalyticsAvailable()) {
ga('create', 'UA-59609997-2', 'auto', 'hrTracker');
}
};
const trackSubmit = function(aet, ant, hrmax) {
if (isAnalyticsAvailable()) {
let url = document.location.pathname + `?hrmax=${hrmax}&ant=${ant}&aet=${aet}`
ga('hrTracker.send', 'pageview', url);
}
};
initAnalytics();
});