-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
112 lines (104 loc) · 3.37 KB
/
app.js
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
const maxDisplayLength = 9;
let currentOperation = '';
let previousValue = null;
let displayString = '0';
const displayValue = () => (displayString.includes('.') ? Number.parseFloat(displayString) : Number.parseInt(displayString));
function setDisplayValue (value) {
displayString = `${value}`;
displayString = (displayString.length > maxDisplayLength && !displayString.includes('.')) ? 'overflow' : displayString.slice(0, maxDisplayLength);
}
function setCurrentOperation(op) {
currentOperation = op;
const faIcon = {'+': 'fa-plus','-':'fa-minus', '*': 'fa-times', '/': 'fa-divide'};
document.getElementById('previousDisplay').innerHTML = (op === '') ? '' : `${previousValue} ${op}`;
document.getElementById('currentOperation').innerHTML = (op === '') ? '' : `<i class="fas ${faIcon[op]}"></i>`;
document.getElementById('currentOperation').style.backgroundColor = (op === '') ? '' : '#00ee00';
document.getElementById('resultFlag').style.backgroundColor = (op === '') ? '#00ee00' : '';
}
function keypress(event) {
let char = String.fromCharCode(event.charCode);
if (/[+\-*/]/.test(char)) {
clickBinaryOp(char);
} else if (/[\d\.]/.test(char)) {
clickFigure(char);
}
}
function keydown(event) {
if (event.keyCode === 13) {
clickUnaryOp('=');
} else if (event.keyCode === 8) {
clickUnaryOp('backspace');
} else if (event.keyCode === 27) {
clickUnaryOp('C');
}
}
function clickFigure(figure) {
displayString = Number.isNaN(displayValue()) ? '' : displayString;
displayString = displayString === '0' ? '' : displayString;
figure = (figure === '.' && displayString.includes('.')) ? '' : figure;
displayString += figure;
displayString = (displayString === '.') ? '0.' : displayString.slice(0, maxDisplayLength);
document.getElementById('resultFlag').style.backgroundColor = "";
document.getElementById('display').innerHTML = displayString;
}
function clickBinaryOp(op) {
if (Number.isNaN(displayValue())) {
return;
}
if (currentOperation !== '') {
resolve();
}
previousValue = displayValue();
displayString = '0';
setCurrentOperation(op);
}
function resolve() {
let result = displayValue();
switch (currentOperation){
case '+':
result = previousValue + displayValue();
break;
case '-':
result = previousValue - displayValue();
break;
case '*':
result = previousValue * displayValue();
break;
case '/':
result = previousValue / displayValue();
break;
}
setDisplayValue(result);
setCurrentOperation('');
}
function clickUnaryOp(op) {
displayString = Number.isNaN(displayValue()) ? '0' : displayString;
switch (op) {
case 'C':
setCurrentOperation('');
case 'CE':
displayString = '0';
break;
case '+-':
setDisplayValue(-displayValue());
break;
case 'backspace':
displayString = displayString.length === 1 ? '0' : displayString.slice(0,displayString.length-1);
displayString = (displayString === '-') ? '0' : displayString;
break;
case "sqrt":
setDisplayValue(Math.sqrt(displayValue()));
break;
case "^2":
setDisplayValue(Math.pow(displayValue(), 2));
break;
case "=":
resolve();
break;
case "%":
resolve();
setDisplayValue(displayValue() / 100);
break;
}
document.getElementById('display').innerHTML = displayString;
}