-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
454 lines (438 loc) · 16.8 KB
/
Copy pathindex.html
File metadata and controls
454 lines (438 loc) · 16.8 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
<!doctype html>
<html>
<head>
<link rel="icon" href="favicon.ico">
<title>
Calculator
</title>
<script src="https://epicenterprograms.github.io/standards/behavior/general.js"></script>
<script>
var S = Standards.general;
var intervals = {};
var keys = {};
var position = {left:0, top:0};
var previousEvaluation = ""; // the previous mathematical expression calculated (prevents spamming Entity creation)
var previousAnswer = ""; // the result of the current/previous calculation
window.addEventListener("load", function() { // This is what runs when the page loads; equivalent to <body onload=aFunction()>
intervals.checker = setInterval(keyResponse, 10);
});
document.addEventListener("keydown", function(event) { // This is when a key is pressed down. This is continually called.
if (document.activeElement != document.getElementById("expression")) { // allows navigating the expression input (and do it without moving stuff)
if ([37, 38, 39, 40].some(function (code) { return event.keyCode == code; })) { // if an arrow key was pressed
event.preventDefault();
}
keys[String(event.keyCode)] = true;
} else if (event.keyCode == 13) { // if the "Enter" key is pressed
event.preventDefault();
solve();
}
});
document.addEventListener("keyup", function(event) { // This is when a key is let up.
keys[String(event.keyCode)] = false;
});
function keyResponse() {
S.forEach(keys, function (trueFalse, key) {
if (trueFalse == true) {
switch (Number(key)) {
case 37: // left arrow key is pressed
position.left--;
break;
case 38: // up arrow key is pressed
position.top--;
break;
case 39: // right arrow key is pressed
position.left++;
break;
case 40: // down arrow key is pressed
position.top++;
break;
}
}
});
document.getElementById("dot").style.left = position.left / 10.0 + "em";
document.getElementById("dot").style.top = position.top / 10.0 + "em";
}
document.addEventListener("mouseup", function(){
document.getElementById("inputOutput").style.background = hexSet([255*Math.round(Math.random()), 255*Math.round(Math.random()), 255*Math.round(Math.random())], 2, '#');
});
var Entity = function (specs) {
specs = specs || {};
this.appearance = {"background-color": String(hexSet([255*Math.round(Math.random()), 255*Math.round(Math.random()), 255*Math.round(Math.random())], 2, '#'))};
this.position = {"top": String((document.body.clientHeight-40)*Math.random()) + "px"};
S.forEach(specs, function(value, specification) {
switch(specification) {
case "appearance":
S.forEach(value, function(feature, key) {
this.appearance[key] = feature;
});
break;
case "position":
S.forEach(value, function(place, key) {
this.position[key] = place;
});
break;
}
});
let div = document.createElement("div");
div.className = "phrase-bullet";
document.body.appendChild(div); // Actually makes the div appear.
S.forEach(this.appearance, function (style, attribute) {
div.style[attribute] = style;
});
S.forEach(this.position, function (style, attribute) {
div.style[attribute] = style;
});
let messages = ["Wahoo!", "Yipee!", "Numbers rule!", "Show math who's boss!", "You + Calculator =<br>AWESOMENESS!", "3 + 3 = 3!"];
div.innerHTML = messages[Math.floor(Math.random()*messages.length)];
setTimeout(function() {
div.style.left = "-25em"; // This doesn't happen immediately because of the transition.
}, 0);
setTimeout(function() {
document.body.removeChild(div);
}, 5000); // This is the duration of the transition.
div.addEventListener("click", function () {
let score = Math.round(25 / this.textContent.length);
if (score == 0) {
score = 1;
}
if (document.getElementById("scoreBox").offsetParent === null) { // checks whether #scoreBox is displayed
document.getElementById("scoreBox").style.display = "inline-block";
document.getElementById("score").innerHTML = "Score: " + score;
} else {
let text = document.getElementById("score").innerHTML;
document.getElementById("score").innerHTML = "Score: " + (Number(text.split(" ")[1]) + score);
}
});
}
function solve() {
let expression = document.getElementById("expression").value.trim().toLowerCase();
// gets rid of anything that isn't math
expression = expression.replace(/(?:(?:the )?answer to )?life,? the universe,? (?:and|&) everything|answer|ans|arccosine|arcsine|arctangent|cosine|sine|tangent|arccos|arcsin|arctan|acos|asin|atan|cos|sin|tan|sqrt|log|ln|pi|e|[^\d .,()+*^%/=<>-]/g, function (match) {
switch (match) {
case (match.match(/(?:(?:the )?answer to )?life,? the universe,? (?:and|&) everything/) ? match.match(/(?:(?:the )?answer to )?life,? the universe,? (?:and|&) everything/)[0] : ""):
case "answer":
case "ans":
case "arccosine":
case "arcsine":
case "arctangent":
case "cosine":
case "sine":
case "tangent":
case "arccos":
case "arcsin":
case "arctan":
case "acos":
case "asin":
case "atan":
case "cos":
case "sin":
case "tan":
case "sqrt":
case "log":
case "ln":
case "pi":
case "e":
return match;
default:
return "";
}
});
if (expression != document.getElementById("expression").value.trim().toLowerCase()) {
alert("Invalid characters were used in the mathematical expression.");
} else if (expression == "") {
return;
} else if (expression.search(/\(|\)/) > -1 && expression.match(/\(/g).length != expression.match(/\)/g).length) {
alert("There's at least one parenthesis missing.");
} else {
expression = expression.replace(/(?:(?:the )?answer to )?life,? the universe,? (?:and|&) everything/g, "42");
expression = expression.replace(/answer|ans/g, previousAnswer);
expression = expression.replace(/,/g, "");
expression = expression.replace(/==+/g, "=");
expression = expression.replace(/([^<>])=(?!<|>)/g, function (match) { return match + "="; });
expression = expression.replace(/=<|==</g, "<=");
expression = expression.replace(/=>|==>/g, ">=");
expression = expression.replace(/\^/g, "**");
expression = expression.replace(/sqrt\(((?:[^()]*(?:\([^()]+\))?[^()]*)+)\)/g, "($1)**.5");
expression = expression.replace(/(^|[^a-z])pi(?![a-z])/g, function (match, char) { return char + "Math.PI"; });
expression = expression.replace(/(^|[^a-z])e(?![a-z])/g, function (match, char) { return char + "Math.E"; });
expression = expression.replace(/(^|[^a-z])(log[^()]*)\(((?:[^()]*(?:\([^()]+\))?[^()]*)+)\)/g, function (match, char, base, log) {
if (base == "log") {
return char + "Math.log(" + log + ")/Math.log(10)";
} else {
return char + "Math.log(" + log + ")/Math.log(" + eval(base.slice(3)) + ")";
}
});
expression = expression.replace(/(^|[^a-z])ln(\d+\.?\d*|\.\d+|\((?:[^()]*(?:\([^()]+\))?[^()]*)+\))/g, function (match, char, log) {
return char + "Math.log(" + (log[0]=="(" ? log.slice(1,log.length-1) : log) + ")";
});
expression = expression.replace(/(^|[^a-z])(?:arccosine|arccos|acos)\(((?:[^()]*(?:\([^()]+\))?[^()]*)+)\)/g, function (match, char, acos) {
return char + "Math.acos(" + acos + (document.getElementById("degrees").checked ? ")*180/Math.PI" : ")");
});
expression = expression.replace(/(^|[^a-z])(?:arcsine|arcsin|asin)\(((?:[^()]*(?:\([^()]+\))?[^()]*)+)\)/g, function (match, char, asin) {
return char + "Math.asin(" + asin + (document.getElementById("degrees").checked ? ")*180/Math.PI" : ")");
});
expression = expression.replace(/(^|[^a-z])(?:arctangent|arctan|atan)\(((?:[^()]*(?:\([^()]+\))?[^()]*)+)\)/g, function (match, char, atan) {
return char + "Math.atan(" + atan + (document.getElementById("degrees").checked ? ")*180/Math.PI" : ")");
});
expression = expression.replace(/(^|[^a-z])(?:cosine|cos)\(((?:[^()]*(?:\([^()]+\))?[^()]*)+)\)/g, function (match, char, cos) {
return char + "Math.cos(" + (document.getElementById("degrees").checked ? cos+"*Math.PI/180" : cos) + ")";
});
expression = expression.replace(/(^|[^a-z])(?:sine|sin)\(((?:[^()]*(?:\([^()]+\))?[^()]*)+)\)/g, function (match, char, sin) {
return char + "Math.sin(" + (document.getElementById("degrees").checked ? sin+"*Math.PI/180" : sin) + ")";
});
expression = expression.replace(/(^|[^a-z])(?:tangent|tan)\(((?:[^()]*(?:\([^()]+\))?[^()]*)+)\)/g, function (match, char, tan) {
return char + "Math.tan(" + (document.getElementById("degrees").checked ? tan+"*Math.PI/180" : tan) + ")";
});
console.log(expression);
try {
let answer = String(eval(expression));
previousAnswer = answer;
if (answer.includes("e") && answer != "true" && answer != "false") { // if the answer is in scientific notation
if (answer != String(Math.round(Number(answer.split("e")[0]) * 1000000) / 1000000)) { // if the significand goes to more than 6 decimal places
document.getElementById("box3").value = Math.round(Number(answer.split("e")[0]) * 1000000) / 1000000;
} else {
document.getElementById("box3").value = answer.split("e")[0];
}
document.getElementById("exponent").textContent = answer.split("e")[1];
document.getElementById("tenToAPower").style.display = "inline-block";
} else if (answer.includes(".") && answer.indexOf(".") > 10) { // if the answer should be in scientific notation and has a decimal
let exponent = answer.indexOf(".") - 1;
document.getElementById("box3").value = answer.splice(exponent + 1, 1).splice(1, 0, ".");
document.getElementById("exponent").textContent = exponent;
document.getElementById("tenToAPower").style.display = "inline-block";
} else if (!answer.includes(".") && answer.length > 10) { // if the answer should be in scientific notation and doesn't have a decimal
let exponent = answer.length - 1;
document.getElementById("box3").value = answer.splice(1, 0, ".");
document.getElementById("exponent").textContent = exponent;
document.getElementById("tenToAPower").style.display = "inline-block";
} else if ((!answer.includes(".") && answer.length > 3 || answer.includes(".") && answer.indexOf(".") > 3) && !(answer === "true" || answer === "false")) { // if comma separators should be added
if (answer.includes(".")) { // if there's a decimal
for (let index = answer.indexOf(".") - 3; index > 0; index -= 3) {
answer = answer.slice(0, index) + "," + answer.slice(index);
}
} else { // if there's not a decimal
for (let index = answer.length - 3; index > 0; index -= 3) {
answer = answer.slice(0, index) + "," + answer.slice(index);
}
}
document.getElementById("box3").value = answer;
document.getElementById("tenToAPower").style.display = "none";
} else { // if the answer doesn't need reformatting
document.getElementById("box3").value = answer;
document.getElementById("tenToAPower").style.display = "none";
}
if (answer == "true") {
document.getElementById("box3").style.color = "#0a0";
} else if (answer == "false") {
document.getElementById("box3").style.color = "red";
} else {
document.getElementById("box3").style.color = "black";
}
makeEntity();
} catch (error) {
console.error(error);
alert("There was a problem interpreting your expression.");
}
}
}
function clearValue() { // This can't be called "clear()".
document.getElementById("expression").value = "";
document.getElementById("box3").value = "";
document.getElementById("tenToAPower").style.display = "none";
keys["39"] = true;
setTimeout(function() {
keys["39"] = false;
keys["40"] = true;
setTimeout(function() {
keys["40"] = false;
keys["37"] = true;
setTimeout(function() {
keys["37"] = false;
keys["38"] = true;
setTimeout(function() {
keys["38"] = false;
keys["39"] = true;
setTimeout(function() {
keys["39"] = false;
position.left = 0;
position.top = 0;
}, 1000);
}, 1100);
}, 2000);
}, 1100);
}, 1000);
makeEntity();
}
S.listen("expression", "dblclick", function () {
this.value = "";
});
S.listen("expression", "touchhold", function () {
this.value = "";
});
function transfer() {
if (previousEvaluation == S.getId("expression").value.trim()) { // if starting to a new expression
S.getId("expression").value = "ans";
} else { // if adding to an existing expression
S.getId("expression").value += "ans";
}
S.getId("expression").focus();
}
function makeEntity() {
if (previousEvaluation != S.getId("expression").value.trim()) {
new Entity();
previousEvaluation = S.getId("expression").value.trim();
}
}
function hexSet(numberList, digits, prefix) {
var result = "";
S.forEach(numberList, function(number) {
number = Math.round(number);
number = number.toString(16);
while (number.length < digits) {
number = "0" + number;
}
result += number;
});
return (prefix || "") + result;
}
</script>
<link rel="stylesheet" href="https://epicenterprograms.github.io/standards/formatting/foundation.css">
<style>
body {
min-height: auto;
padding: 0em;
background: linear-gradient(to bottom right, red, orange, yellow, green, blue, purple);
}
button {
margin: .25em;
}
.morph {
border: .05em solid #aaaaaa;
}
.morph input {
padding: .15em;
}
.morph:hover {
border-radius: 1em;
}
.phrase-bullet {
position: absolute;
left: 100vw;
border-radius: 1em 0em 0em 1em;
padding: .5em;
color: white;
font-size: 2em;
text-shadow: 0em 0em .1em black, 0em 0em .1em black;
white-space: nowrap;
cursor: pointer;
-webkit-transition: left 5s linear;
-ms-transition: left 5s linear;
transition: left 5s linear;
}
#title {
color: black;
text-decoration: none;
}
#title:hover {
color: blue;
text-decoration: underline;
}
#inputOutput {
position: relative;
border: .1em solid black;
background: blue;
width: 15em;
height: 15em;
}
#expression {
margin: .75em auto;
padding: .3em;
width: 12em;
height: 3em;
}
#answer {
position: absolute;
left: 4.25em;
bottom: 1em;
margin: auto;
background: white;
}
#answer input:first-child {
width: 8em;
}
#tenToAPower {
display: none;
}
#dot {
position: relative;
padding: 1rem;
background: blue;
border-radius: 1rem;
margin: 0rem auto 0em;
}
#scoreBox {
display: none;
padding: 1.5em;
background: radial-gradient(ellipse, white, transparent, transparent);
-webkit-animation: pulse 1s ease infinite 0s alternate;
animation: pulse 1s ease infinite 0s alternate;
}
#scoreHolder {
padding-top: 3em;
width: 8em;
height: 8em;
}
@keyframes pulse {
from {
margin: 2em;
padding: 1.5em;
}
to {
margin: 0em;
padding: 3.5em;
}
}
</style>
</head>
<body>
<h1>
<a href="https://epicenterprograms.github.io/calculator/turnedInVersion" title="Click for awesomeness" id="title">
Calculator
</a>
</h1>
<div id="inputOutput">
<textarea class="morph" id="expression" placeholder="Type your expression"></textarea>
<div id="answer">
<input type="text" id="box3" readonly placeholder="Result">
<div id="tenToAPower">
× 10<sup id="exponent">0</sup>
</div>
</div>
<br>
<div id="dot">
<button class="morph" onclick="solve()">
=
</button>
<button class="morph" onclick="transfer()">
Ans
</button>
<br>
<button class="morph" onclick="clearValue()">
Clear
</button>
<br>
<input type="radio" name="trigUnits" id="degrees" checked>
<label for="radians">Degrees</label>
<input type="radio" name="trigUnits" id="radians">
<label for="radians">Radians</label>
</div>
</div>
<br>
<div id="scoreBox">
<div id="scoreHolder">
<span id="score"></span><br>
<span id="highscore"></span>
</div>
</div>
</body>
</html>