-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpleCalculator.js
146 lines (129 loc) · 4.04 KB
/
simpleCalculator.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
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
var calculator = document.querySelector('.calculator');
var display = calculator.querySelector('.calculator-screen');
var keys = calculator.querySelector('.calculator-keys');
keys.addEventListener('click', function (e) {
if (!e.target.matches('button')) return;
var key = e.target;
var displayedNum = display.textContent;
var resultString = getResultString(key, displayedNum, calculator.dataset);
display.textContent = resultString;
updateCalculatorState(key, calculator, resultString, displayedNum);
updateVisualState(key, calculator);
});
//Method to calculate operands with operator
function calculate(num1, operator, num2) {
var firstNumber = parseFloat(num1);
var secondNumber = parseFloat(num2);
switch(operator) {
case 'add':
return firstNumber + secondNumber;
case 'subtract':
return firstNumber - secondNumber;
case 'multiply':
return firstNumber * secondNumber;
case 'divide':
return firstNumber / secondNumber;
default:
break;
}
}
//Method to get key type
function getKeyType(key) {
var action = key.dataset.action;
if (!action) {
return 'number';
}
switch(action) {
case 'add':
case 'subtract':
case 'multiply':
case 'divide':
return 'operator';
default:
return action;
}
}
//Method to perform operation and get the result string
function getResultString(key, displayedNum, state) {
var keyContent = key.textContent;
var keyType = getKeyType(key);
var firstValue = state.firstValue;
var operator = state.operator;
var modValue = state.modValue;
var previousKeyType = state.previousKeyType;
if (keyType === 'number') {
return displayedNum === '0' ||
previousKeyType === 'operator' ||
previousKeyType === 'calculate' ?
keyContent :
displayedNum + keyContent;
}
if (keyType === 'decimal') {
if (!displayedNum.includes('.')) return displayedNum + '.';
if (previousKeyType === 'operator' || previousKeyType === 'calculate') return '0.';
return displayedNum;
}
if (keyType === 'operator') {
return firstValue &&
operator &&
previousKeyType !== 'operator' &&
previousKeyType !== 'calculate' ?
calculate(firstValue, operator, displayedNum) :
displayedNum;
}
if (keyType === 'clear') {
return 0;
}
if (keyType === 'calculate') {
return firstValue ?
previousKeyType === 'calculate' ?
calculate(displayedNum, operator, modValue) :
calculate(firstValue, operator, displayedNum) :
displayedNum;
}
}
function updateCalculatorState(key, calculator, calculatedValue, displayedNum) {
var keyType = getKeyType(key);
var _calculator$dataset = calculator.dataset;
var firstValue = _calculator$dataset.firstValue;
var operator = _calculator$dataset.operator;
var modValue = _calculator$dataset.modValue;
var previousKeyType = _calculator$dataset.previousKeyType;
calculator.dataset.previousKeyType = keyType;
if (keyType === 'operator') {
calculator.dataset.operator = key.dataset.action;
calculator.dataset.firstValue = firstValue &&
operator &&
previousKeyType !== 'operator' &&
previousKeyType !== 'calculate' ?
calculatedValue :
displayedNum;
}
if (keyType === 'calculate') {
calculator.dataset.modValue = firstValue && previousKeyType === 'calculate' ?
modValue :
displayedNum;
}
if (keyType === 'clear' && key.textContent === 'AC') {
calculator.dataset.firstValue = '';
calculator.dataset.modValue = '';
calculator.dataset.operator = '';
calculator.dataset.previousKeyType = '';
}
}
function updateVisualState(key, calculator) {
var keyType = getKeyType(key);
Array.from(key.parentNode.children).forEach(function (k) {
return k.classList.remove('is-depressed');
});
if (keyType === 'operator') {
key.classList.add('is-depressed');
}
if (keyType === 'clear' && key.textContent !== 'AC') {
key.textContent = 'AC';
}
if (keyType !== 'clear') {
var clearButton = calculator.querySelector('[data-action=clear]');
clearButton.textContent = 'CE';
}
}