-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
62 lines (55 loc) · 1.69 KB
/
script.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
let result = [0];
const displayContainer = document.querySelector('div.display');
function getLastResultValue() {
return result[result.length -1];
}
function numberClicked(number) {
const lastResult = getLastResultValue();
if (typeof lastResult === 'bigint' || typeof lastResult === 'number') {
result[result.length -1] = parseFloat(lastResult.toString() + number);
} else if (typeof lastResult === 'string') {
result.push(number);
}
refreshShownValue();
}
function operandClicked(operand) {
const lastResult = getLastResultValue();
if (typeof lastResult === 'bigint' || typeof lastResult === 'number') {
result.push(operand);
} else if (typeof lastResult === 'string') {
result[result.length -1] = operand;
}
refreshShownValue();
}
function clearClicked(all) {
if (all) {
result = [0n];
return refreshShownValue();
}
const lastResult = getLastResultValue();
if (typeof lastResult === 'bigint' || typeof lastResult === 'number') {
if (lastResult.toString().length === 1) {
if (result.length === 1) {
result = [0n];
return refreshShownValue();
}
result = result.slice(0, -1);
return refreshShownValue();
}
result[result.length -1] = parseFloat(lastResult.toString().slice(0, -1));
return refreshShownValue();
}
if (typeof lastResult === 'string') {
result = result.slice(0, -1);
return refreshShownValue();
}
}
function refreshShownValue() {
displayContainer.innerHTML = result.join(' ');
}
function forceResult() {
const localResult = eval(result.join(''));
result = localResult % 1 !== localResult ? [localResult] : [parseFloat(localResult)];
refreshShownValue();
}
refreshShownValue();