forked from a-a-ron/ps-github-foundations-github-copilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
224 lines (186 loc) · 4.66 KB
/
client.js
File metadata and controls
224 lines (186 loc) · 4.66 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
"use strict";
var value = 0;
var states = {
start: 0,
operand1: 1,
operator: 2,
operand2: 3,
complete: 4,
};
var state = states.start;
var operand1 = 0;
var operand2 = 0;
var operation = null;
function calculate(operand1, operand2, operation) {
var uri = location.origin + "/arithmetic";
switch (operation) {
case "+":
uri += "?operation=add";
break;
case "-":
uri += "?operation=subtract";
break;
case "*":
uri += "?operation=multiply";
break;
case "/":
uri += "?operation=divide";
break;
case "^":
uri += "?operation=power";
break;
default:
setError();
return;
}
uri += "&operand1=" + encodeURIComponent(operand1);
uri += "&operand2=" + encodeURIComponent(operand2);
setLoading(true);
var http = new XMLHttpRequest();
http.open("GET", uri, true);
http.onload = function () {
setLoading(false);
if (http.status == 200) {
var response = JSON.parse(http.responseText);
setValue(response.result);
} else {
setError();
}
};
http.send(null);
}
function clearPressed() {
setValue(0);
operand1 = 0;
operand2 = 0;
operation = null;
state = states.start;
}
function clearEntryPressed() {
setValue(0);
state = state == states.operand2 ? states.operator : states.start;
}
function numberPressed(n) {
var value = getValue();
if (state == states.start || state == states.complete) {
value = n;
state = n == "0" ? states.start : states.operand1;
} else if (state == states.operator) {
value = n;
state = n == "0" ? states.operator : states.operand2;
} else if (value.replace(/[-\.]/g, "").length < 8) {
value += n;
}
value += "";
setValue(value);
}
function decimalPressed() {
if (state == states.start || state == states.complete) {
setValue("0.");
state = states.operand1;
} else if (state == states.operator) {
setValue("0.");
state = states.operand2;
} else if (!getValue().toString().includes(".")) {
setValue(getValue() + ".");
}
}
function signPressed() {
var value = getValue();
if (value != 0) {
setValue(-1 * value);
}
}
function expoPressed() {
// unary exponential: compute e^(current value)
var current = getValue();
var n = Number(current);
if (isNaN(n)) {
setError();
return;
}
setLoading(true);
try {
var result = Math.exp(n);
setValue(result);
state = states.complete;
} catch (e) {
setError();
} finally {
setLoading(false);
}
}
function operationPressed(op) {
operand1 = getValue();
operation = op;
state = states.operator;
}
function equalPressed() {
if (state < states.operand2) {
state = states.complete;
return;
}
if (state == states.operand2) {
operand2 = getValue();
state = states.complete;
} else if (state == states.complete) {
operand1 = getValue();
}
calculate(operand1, operand2, operation);
}
// TODO: Add key press logics
document.addEventListener("keypress", (event) => {
if (event.key.match(/^\d+$/)) {
numberPressed(event.key);
} else if (event.key == ".") {
decimalPressed();
} else if (event.key.match(/^[-*+/]$/)) {
operationPressed(event.key);
} else if (event.key == "=") {
equalPressed();
}
});
function getValue() {
return value;
}
function setValue(n) {
value = n;
var displayValue = value;
if (displayValue > 99999999) {
displayValue = displayValue.toExponential(4);
} else if (displayValue < -99999999) {
displayValue = displayValue.toExponential(4);
} else if (displayValue > 0 && displayValue < 0.0000001) {
displayValue = displayValue.toExponential(4);
} else if (displayValue < 0 && displayValue > -0.0000001) {
displayValue = displayValue.toExponential(3);
}
var chars = displayValue.toString().split("");
var html = "";
for (var c of chars) {
if (c == "-") {
html += '<span class="resultchar negative">' + c + "</span>";
} else if (c == ".") {
html += '<span class="resultchar decimal">' + c + "</span>";
} else if (c == "e") {
html += '<span class="resultchar exponent">e</span>';
} else if (c != "+") {
html += '<span class="resultchar digit' + c + '">' + c + "</span>";
}
}
document.getElementById("result").innerHTML = html;
}
function setError(n) {
document.getElementById("result").innerHTML = "ERROR";
}
function setLoading(loading) {
if (loading) {
document.getElementById("loading").style.visibility = "visible";
} else {
document.getElementById("loading").style.visibility = "hidden";
}
var buttons = document.querySelectorAll("BUTTON");
for (var i = 0; i < buttons.length; i++) {
buttons[i].disabled = loading;
}
}