forked from Anagrr/Fuse.NeuCalculator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainView.js
More file actions
120 lines (105 loc) · 3.01 KB
/
MainView.js
File metadata and controls
120 lines (105 loc) · 3.01 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
var Observable = require('FuseJS/Observable');
var Keyboard = require('Keyboard').Keyboard;
var FirstOutput = Observable();
var SecondOutput = Observable();
var Operation = Observable();
resetAll();
function onKeyPressed(args) {
var value = args.data.title;
switch(value) {
case 'AC' :
resetAll();
break;
case '+/-' :
FirstOutput.value = FirstOutput.value.startsWith('-')
? FirstOutput.value.replace('-','')
: `-${FirstOutput.value}`;
break;
case '%' :
case '/' :
case 'x' :
case '+' :
case '-' :
onOperationButtonPressed(value);
break;
case '=' :
if(!Operation.value || IsEmptyFirstOutput()) return;
calculate();
break;
case '0':
if(!IsEmptyFirstOutput()) {
FirstOutput.value = `${FirstOutput.value}${value}`;
}
break;
case '.':
if(!FirstOutput.value.includes('.')) {
FirstOutput.value = `${FirstOutput.value}${value}`;
}
break;
default :
if(IsMaxLength()) return;
FirstOutput.value = `${IsEmptyFirstOutput() ? '' : FirstOutput.value}${value}`;
}
}
function IsEmptyFirstOutput() {
return FirstOutput.value == "0";
}
function IsEmptySecondOutput() {
return SecondOutput.value.length == 0;
}
function IsMaxLength() {
return FirstOutput.value.length >= 8
}
function resetAll() {
resetFirstOutput();
Operation.value = "";
SecondOutput.value = "";
};
function resetFirstOutput() {
FirstOutput.value = "0";
};
function onOperationButtonPressed(operation) {
Operation.value = operation;
if(IsEmptySecondOutput()) {
SecondOutput.value = `${FirstOutput.value}`;
resetFirstOutput();
}
};
function calculate() {
var result = 0;
var firstValue = SecondOutput.value.includes('.') ? Number.parseFloat(SecondOutput.value) : Number.parseInt(SecondOutput.value);
var secondValue = FirstOutput.value.includes('.') ? Number.parseFloat(FirstOutput.value) : Number.parseInt(FirstOutput.value);
switch(Operation.value) {
case '%' :
result = (firstValue / 100) * secondValue;
break;
case '/' :
result = firstValue / secondValue;
break;
case 'x' :
result = firstValue * secondValue;
break;
case '+' :
result = firstValue + secondValue;
break;
case '-' :
result = firstValue - secondValue;
break;
}
resetAll();
FirstOutput.value = formatResult(`${result}`);
};
function formatResult(result) {
var formatted = '';
for(var i=0; i < result.length; i++) {
formatted += i % 8 == 7 ? `${result[i]} ` : result[i];
}
return formatted;
};
module.exports = {
FirstOutput,
SecondOutput,
Keyboard,
Operation,
onKeyPressed
};