-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
151 lines (126 loc) · 4.83 KB
/
script.js
File metadata and controls
151 lines (126 loc) · 4.83 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
function toggleDropDown() {
// either hide or display the dropdown menu
var menu = document.getElementById("dropdown-content");
if (menu.style.display == "none") {
// unhide it
menu.style.display = "block";
} else {
menu.style.display = "none";
}
}
/* possible optimizations!
- caching total for averages so that after each solve we just have to subtract the oldest time and add the newest time and divide by (5, 12, 100)
(would require recomputing after deletion of value, but would still save time overall in terms of amortized performance)
*/
var timesList = [];
var numSolves = 0;
function addTime(time) {
// add this time to the list of recorded times
timesList.push(parseFloat(time));
if (getComputedStyle(document.getElementById("timesList"), null).height == "0px") {
// not yet been set; set the height of the element before pushing new time onto the list display
setTimeListHeight();
}
appendTimeHTML(time);
numSolves += 1;
// also update averages
updateAvg(3);
updateAvg(5);
updateAvg(12);
updateAvg(100);
}
function deleteTime(index) {
// delete the value at the specified index (if that index exists in the list)
timesList.splice(index, 1);
// also remove from displayed list
var times = document.getElementById("timesList").childNodes;
for (var i = 0; i < times.length; i++) {
if (i == 1) {
console.log("!!! " + typeof times[i].children);
for (var j = 0; j < times[i].children.length; j++) {
console.log(times[i].children[j]);
}
}
console.log(times[i]);
}
var tableRows = times[1].children;
// we have to reverse the index because the newest values (with highest indeces) are at smallest positions in list; if there are N solves in the list so far, then tableRows[0] is the Nth solve, tableRows[1] is the N-1st solve, and so on. To delete the Kth solve, delete the (N-K)th index.
var rowToDelete = tableRows[timesList.length - index + 1];
times[1].removeChild(rowToDelete);
// re-enumerate the times starting from where we deleted
console.log("88888");
for (var i = timesList.length - index + 1; i >= 0; i--) {
// shift all the numbers down by 1
var rowIndex = tableRows[i];
console.log(rowIndex);
rowIndex.innerHTML = parseInt(rowIndex.innerHTML) - 1;
}
}
function updateAvg(N) {
// update the aoN row in the averages table
if (N == 3) {
var avg = mo3();
var tag = "mo3";
} else {
var avg = aoN(N);
var tag = "ao" + N;
}
document.getElementById(tag).innerHTML = avg;
}
function aoN(N) {
// return the average over the last N solves
if (N > timesList.length) {
return "--";
}
var lastN = timesList.slice(-1*N);
console.log(N, average(lastN));
return average(lastN);
}
function mo3() {
// return the mean of the middle 3 solves out of the last 5 (drop the fastest and slowest time)
var lastFive = timesList.slice(-5);
var top = Math.max.apply(null, lastFive);
var bottom = Math.min.apply(null, lastFive);
// remove the top and bottom elements
lastFive.splice(lastFive.indexOf(top), 1 );
lastFive.splice(lastFive.indexOf(bottom), 1 );
// average the new list
return average(lastFive);
}
function appendTimeHTML(time) {
// append a time to the list being displayed onscreen
var tab = document.getElementById("timesListTable");
var newRow = document.createElement("tr");
var indexEntry = document.createElement("th");
var indexNum = document.createTextNode(timesList.length);
indexEntry.appendChild(indexNum);
indexEntry.setAttribute("class", "tg-0lax");
// truncate the time to fit in the box neatly
var timeEntry = document.createElement("th");
var timeVal = document.createTextNode(time);
timeEntry.appendChild(timeVal);
timeEntry.setAttribute("class", "tg-0lax");
newRow.appendChild(indexEntry);
newRow.appendChild(timeEntry);
tab.insertBefore(newRow, tab.childNodes.item(0));
}
function average(arr) {
// return the average of an input array
if (arr.length == 0) {
return 0;
}
var total = 0;
for (var i = 0; i < arr.length; i++) {
total += arr[i];
}
return total/arr.length;
}
function setTimeListHeight() {
// compute and set appropriate height for times list box according to page size and set it
var height = document.documentElement.scrollHeight;
var topPos = getComputedStyle(document.getElementById("timesList"), null).top;
// console.log(height);
topPos = parseInt(topPos.slice(0, topPos.length-2));
var newHeight = (height - topPos - 0.01*height)+ "px";
document.getElementById("timesList").style.height = newHeight;
}