-
Notifications
You must be signed in to change notification settings - Fork 0
/
static.html
340 lines (296 loc) · 10.6 KB
/
static.html
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
<!DOCTYPE html>
<html class="no-js">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Chaining Calculator</title>
<meta name="description" content="chain functions">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
/* general styling */
html, body {
margin: 0px;
width: 100%;
height: 100%;
display: flex;
}
.flex {
display: flex;
}
.col-1 {
flex: 75;
}
.col-2 {
flex: 25;
}
.panel {
display: flex;
flex: 1;
justify-content: center;
align-items: center;
height: 100%;
}
.row {
display: block;
}
form {
border: 1px solid #ddd;
}
input,
input:focus,
input:visited,
input:active {
border: none;
background-color: #fff;
outline: none;
cursor: pointer;
}
input:hover {
background-color: #f7e228;
transition: all 1.2s;
}
/* calculator styling */
.area > input[type="text"] {
color: #000;
background-color: #ddd;
text-align: right;
height: 48pt;
font-size: 24pt;
}
.buttons {
display: flex;
}
.button {
flex: 24;
/* border: 1px solid #ddd; */
font-size: 24pt;
padding: 12pt;
}
#showValue {
padding-right: 8pt;
}
.button.reset {
background-color: #f2575b;
}
.buttons.equal {
display: flex;
height: 40%;
}
.buttons.equal > .button {
background-color: #2782bf;
}
/* media query for landscape display (fully responsive) */
@media only screen and (max-width: 768px) and (orientation: landscape) {
.panel {
display: block !important;
}
#showValue {
width: 100%;
padding-right: 0px;
}
.button {
font-size: 18pt;
padding: 8pt;
}
}
</style>
</head>
<body>
<!--
features summary:
1. chaining function is fully working, such as calc.add(6).add(5).subtract(1).multiply(2).divide(1).equals(); [It can be tested on part 1]
2. simple calculator major functionalities have been done [UI + Logic]
3. unit test for testing the (+-*/) major functions
-->
<section class="panel">
<div class="row">
<form>
<div class="area">
<input type="text" id="showValue" value="" disabled />
</div>
<div class="flex">
<div class="col-1">
<div class="buttons">
<input class="button" type="button" onclick="displayValue('+')" value="+" />
<input class="button" type="button" onclick="displayValue('-')" value="−" />
<input class="button" type="button" onclick="displayValue('*')" value="×" />
</div>
<div class="buttons">
<input class="number button" type="button" onclick="displayValue(7)" value="7" />
<input class="number button" type="button" onclick="displayValue(8)" value="8" />
<input class="number button" type="button" onclick="displayValue(9)" value="9" />
</div>
<div class="buttons">
<input class="number button" type="button" onclick="displayValue(4)" value="4" />
<input class="number button" type="button" onclick="displayValue(5)" value="5" />
<input class="number button" type="button" onclick="displayValue(6)" value="6" />
</div>
<div class="buttons">
<input class="number button" type="button" onclick="displayValue(1)" value="1" />
<input class="number button" type="button" onclick="displayValue(2)" value="2" />
<input class="number button" type="button" onclick="displayValue(3)" value="3" />
</div>
<div class="buttons">
<input class="number button" type="button" onclick="displayValue(0)" value="0" />
<input class="number button" type="button" onclick="displayValue('.')" value="." />
<input class="reset button" type="button" onclick="resetValue()" value="C" />
</div>
</div>
<div class="col-2">
<div class="buttons">
<input class="button" type="button" onclick="displayValue('/')" value="÷" />
</div>
<div class="buttons">
<input class="button" type="button" onclick="displayValue('(')" value="(" />
</div>
<div class="buttons">
<input class="button" type="button" onclick="displayValue(')')" value=")" />
</div>
<div class="buttons equal">
<input class="button" type="button" onclick="displayResult()" value="=" />
</div>
</div>
</div>
</form>
</div>
</section>
<script type="text/javascript" src="./scripts/index.js"></script>
<script type="text/javascript">
var isCalculated = false;
// part 1
// basic functions for calculation
var addCalc = function(val1, val2) {
return parseFloat(val1) + parseFloat(val2);
}
var subtractCalc = function(val1, val2) {
return parseFloat(val1) - parseFloat(val2);
}
var multiplyCalc = function(val1, val2) {
return parseFloat(val1) * parseFloat(val2);
}
var divideCalc = function(val1, val2) {
return parseFloat(val1) / parseFloat(val2);
}
var resetCalc = function() {
return '';
}
var equalCalc = function(currentVal) {
return currentVal;
}
// chain function for chaining all calculations together
var Calc = function() {
this.value = 0;
// add function
this.add = function(newValue) {
this.value = addCalc(this.value, newValue);
return this;
}
// subtract function
this.subtract = function(newValue) {
this.value = subtractCalc(this.value, newValue);
return this;
}
// multiply function
this.multiply = function(newValue) {
this.value = multiplyCalc(this.value, newValue);
return this;
}
// divide function
this.divide = function(newValue) {
this.value = divideCalc(this.value, newValue);
return this;
}
// reset function
this.reset = function() {
this.value = resetCalc();
return this;
}
// equals function
this.equals = function() {
this.value = equalCalc(this.value);
return this.value;
}
}
// Chaining function test demo
// var calc = new Calc();
// calc.add(6).add(5).subtract(1).multiply(2).divide(1).equals();
// part 2
// calculator function
var showValueNode = document.getElementById('showValue');
var displayValue = function(val) {
if(this.isCalculated === true && !isNaN(val)) { // if user clicks numeric buttons, reset value to ''
this.isCalculated = false;
showValueNode.value = '';
}
if(this.isCalculated === true && isNaN(val)) { // if user clicks non-numeric buttons, continue calculation process
this.isCalculated = false;
showValueNode.value = document.getElementById('showValue').value;
}
if(this.isCalculated === false) { // normal process
showValueNode.value += val;
}
}
var countDecimals = function(value) { // count decinmal places
if (Math.floor(value) !== value){
return value.toString().split(".")[1].length || 0;
}
return 0;
}
var decimalControl = function(value) {
if(countDecimals(value) > 8) { // fix decimal place value as 8 if calculated value beyonds 8 decimal places
return value.toFixed(8);
}
return value;
}
var displayResult = function() {
var rawStr = showValueNode.value,
copyStr = rawStr;
rawStr = rawStr.replace(/[0-9]+/g, '#').replace(/[\(|\|\.)]/g, '');
var getNumbers = copyStr.split(/[^0-9\.]+/),
operators = rawStr.split('#').filter(function(n){
return n
}),
calcArray = [];
for(let i = 0; i < getNumbers.length; i++) {
calcArray.push(getNumbers[i]);
if (i < operators.length) {
calcArray.push(operators[i]);
}
}
if(calcArray.length <= 3) { // if input format is like value1 + or - or * or / value 2
switch(calcArray[1]) {
case '+':
this.isCalculated = true;
showValueNode.value = decimalControl(addCalc(calcArray[0], calcArray[2])); // using part 1 calc library add method
break;
case '-':
this.isCalculated = true;
showValueNode.value = decimalControl(subtractCalc(calcArray[0], calcArray[2])); // using part 1 calc library subtract method
break;
case '*':
this.isCalculated = true;
showValueNode.value = decimalControl(multiplyCalc(calcArray[0], calcArray[2])); // using part 1 calc library multiply method
break;
case '/':
this.isCalculated = true;
showValueNode.value = decimalControl(divideCalc(calcArray[0], calcArray[2])); // using part 1 calc library divide method
break;
}
}else {
this.isCalculated = true;
showValueNode.value = decimalControl(eval(calcArray.join(' '))); // handle the multiple values calculation methods, such as a * b + c / b
}
}
var resetValue = function() {
showValueNode.value = ''; // reset value to 0
}
// The drawbacks I can think of is that
// I don't have enough time to handle is to reduce the scripts file size
// Calculator still needs more time to complete all the features, not completed yet, just a prototype
// TODO
// To seperate functions into different components, later smaller files will be imported by index.html file (Personal thoughts)
// To make display more advanced (able to handle more than 11 characters calculation display)
</script>
<!-- Total time spent for this task: 6.5 hours -->
</body>
</html>