-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
208 lines (195 loc) · 5.31 KB
/
index.js
File metadata and controls
208 lines (195 loc) · 5.31 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
var running = false;
var interval;
var decimal = 0;
var sec = 0;
var min = 0;
var cs = 0;
var decimalOut = document.getElementById("decimal");
var secOut = document.getElementById("sec");
var minOut = document.getElementById("min");
var colon = document.getElementById("colon");
var timesOut = document.getElementById("timeOut");
var timesList = document.getElementById("timeList");
var clearAll = document.getElementById("clear");
var timesDisplay = new Array();
var csTimes = new Array();
var avAll = 0;
var avAllOut = document.getElementById("overallAv");
var best = 999999999999999999;
var bestOut = document.getElementById("fastest");
var worst = 0;
var numSolves = 0;
var total = 0;
var numSolvesOut = document.getElementById("solveNum");
alert("The clear times button is problematic as of now, so to clear times, please refresh the page, Thank you :)")
generateScramble();
function timer() {
decimal++;
cs++; //counts time in centiseconds
decimalOut.innerHTML = decimal;
if (decimal >= 100) {
decimal = 0;
sec++;
if (sec > 59) {
sec = 0;
min++;
colon.innerHTML = ":";
minOut.innerHTML = min;
}
if (sec <= 9 && min > 0) {
sec = "0" + sec;
}
secOut.innerHTML = sec;
}
if (decimal <= 9) {
decimal = '0' + decimal;
decimalOut.innerHTML = decimal;
}
}
document.addEventListener('keyup', function(event) {
if (event.code === 'Space') {
if (!running) {
decimal = 0;
sec = 0;
min = 0;
cs = 0;
secOut.innerHTML = "0";
minOut.innerHTML = "";
colon.innerHTML = "";
running = true;
scramble = "";
interval = setInterval(timer, 10);
} else if (running) {
running = false;
clearInterval(interval);
timesDisplay.push(" " + timesOut.innerHTML);
csTimes.push(cs);
timesList.innerHTML = timesDisplay;
calculateStats();
generateScramble();
}
}
});
function run() {
if (!running) {
decimal = 0;
sec = 0;
min = 0;
cs = 0;
secOut.innerHTML = "0";
minOut.innerHTML = "";
colon.innerHTML = "";
running = true;
scramble = "";
interval = setInterval(timer, 10);
} else if (running) {
running = false;
clearInterval(interval);
timesDisplay.push(" " + timesOut.innerHTML);
csTimes.push(cs);
timesList.innerHTML = timesDisplay;
calculateStats();
generateScramble();
}
}
function generateScramble() {
var move; //includes face to turn and how to turn it. Ex. 2F
var face; //Face to turn. Either R, L, F, B, U, or D
var faceNum; //1-6, corresponds to face R-D
var lastFaceNum = 10; //The face of the previous turn
var turn; //How to turn a face. Either ', 2, or nothing.
var scramble = ""; //inlucdes 25 moves
var output = document.getElementById("scram");
for (var x = 0; x < 25; x++) {
do {
faceNum = Math.floor(Math.random() * 6) + 1;
} while (faceNum === lastFaceNum); //the same face can't appear in consecutive moves.
lastFaceNum = faceNum;
if (faceNum === 1) {
face = "R";
}
if (faceNum === 2) {
face = "L";
}
if (faceNum === 3) {
face = "U";
}
if (faceNum === 4) {
face = "D";
}
if (faceNum === 5) {
face = "F";
}
if (faceNum === 6) {
face = "B";
}
turn = Math.floor(Math.random() * 3) + 1;
if (turn === 1) {
move = face;
}
if (turn === 2) {
move = face + "2";
}
if (turn === 3) {
move = face + "'";
}
scramble += move + " ";
}
output.innerHTML = scramble;
}
clearAll.onclick = clearTimes;
function clearTimes() {
numSolves = 0;
numSolvesOut.innerHTML = numSolves;
best = 99999999999;
bestOut.innerHTML = "";
worst = 0;
avAll = 0;
total = 0;
avAllOut.innerHTML = "";
timesDisplay = [];
csTimes = [];
timesList.innerHTML = timesDisplay;
}
function calculateStats() {
numSolves++;
total = 0;
numSolvesOut.innerHTML = numSolves;
for (var x = 0; x < csTimes.length; x++) {
if (csTimes[x] < best) {
best = csTimes[x];
}
if (csTimes[x] > worst) {
worst = csTimes[x];
}
total += csTimes[x];
}
avAll = total / numSolves;
avAllOut.innerHTML = formatTime(avAll);
bestOut.innerHTML = formatTime(best);
}
function formatTime(t) {
//m = minute, s = second, c = centisecond
var m = 0,
s = 0,
c = 0,
out = "";
m = Math.floor(t / 6000);
t = t % 6000;
s = Math.floor(t / 100);
t = t % 100;
c = Math.floor(t);
if (m < 1) {
m = "";
} else {
m = m + ":";
if (s < 10) {
s = "0" + s;
}
}
if (c < 10) {
c = "0" + c;
}
out = "" + m + s + "." + c;
return out;
}