-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
252 lines (201 loc) · 7.17 KB
/
index.js
File metadata and controls
252 lines (201 loc) · 7.17 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
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
/**
* @class Model
*
* Manages the data of the application.
*/
class Model {
constructor() {
this.displayValue = '0',
this.waitingForSecondOperand = false,
this.firstOperand = null,
this.operator=null,
this.performCalculation = {
'/': (firstOperand, secondOperand) => firstOperand / secondOperand,
'*': (firstOperand, secondOperand) => firstOperand * secondOperand,
'+': (firstOperand, secondOperand) => firstOperand + secondOperand,
'-': (firstOperand, secondOperand) => firstOperand - secondOperand,
'=': (firstOperand, secondOperand) => secondOperand,
}
}
handleValue(target){
if (!target.matches('button')) {
return;
}
if (target.classList.contains('operator')) {
this.handleOperator(target.value);
return;
}
this.inputDigit(target.value)
}
inputDigit(digit) {
if (this.waitingForSecondOperand === true) {
this.displayValue = digit;
this.waitingForSecondOperand = false;
} else {
this.displayValue = this.displayValue === '0' ? digit : this.displayValue + digit;
}
console.log(this);
}
handleOperator(nextOperator){
const inputValue = parseFloat(this.displayValue);
if (nextOperator === 'all-clear') {
this._resetInput();
return;
}
if(this.firstOperand == null) {
this.firstOperand = inputValue;
} else if (this.operator){
const result = this.performCalculation[this.operator](this.firstOperand,inputValue);
this.displayValue = String(result);
this.firstOperand = result;
}
this.waitingForSecondOperand = true;
this.operator = nextOperator;
console.log(this);
}
_resetInput() {
this.displayValue = '0',
this.waitingForSecondOperand = false,
this.firstOperand = null,
this.operator=null
console.log(this);
}
}//Model
/**
* @class View
*
* Visual representation of the model.
*/
class View {
constructor() {
this.app = this.getElement('#root')
this.calculator = this.createElement('div','calculator')
this.screen = this.createElement('input','calculator-screen')
this.screen.type = "text"
this.screen.value = ""
this.keyScreen = this.createElement('div','calculator-keys')
this.addButton = this.createElement('button','operator')
this.minusButton = this.createElement('button','operator')
this.multiplyButton = this.createElement('button','operator')
this.divideButton = this.createElement('button','operator')
this.SevenButton = this.createElement('button')
this.EightButton = this.createElement('button')
this.NineButton = this.createElement('button')
this.FourButton = this.createElement('button')
this.FiveButton = this.createElement('button')
this.SixButton = this.createElement('button')
this.OneButton = this.createElement('button')
this.TwoButton = this.createElement('button')
this.ThreeButton = this.createElement('button')
this.ZeroButton = this.createElement('button')
this.dotButton = this.createElement('button')
this.clearButton = this.createElement('button','all-clear')
this.calculateButton = this.createElement('button','equal-sign')
//operators
this.addButton.textContent = '+'
this.addButton.value = '+'
this.minusButton.textContent = '-'
this.minusButton.value ='-'
this.multiplyButton.textContent ='*'
this.multiplyButton.value = '*'
this.divideButton.textContent = '/'
this.divideButton.value = '/'
//Number keys
this.NineButton.textContent = '9'
this.NineButton.value = '9'
this.EightButton.textContent = '8'
this.EightButton.value= '8'
this.SevenButton.textContent ='7'
this.SevenButton.value = '7'
this.SixButton.textContent = '6'
this.SixButton.value ='6'
this.FiveButton.textContent ='5'
this.FiveButton.value = '5'
this.FourButton.textContent = '4'
this.FourButton.value = '4'
this.ThreeButton.textContent = '3'
this.ThreeButton.value = '3'
this.TwoButton.textContent ='2'
this.TwoButton.value ='2'
this.OneButton.textContent = '1'
this.OneButton.value = '1'
this.ZeroButton.textContent = '0'
this.ZeroButton.value = '0'
this.dotButton.textContent = '.'
this.dotButton.value = '.'
this.clearButton.textContent = 'AC'
this.clearButton.value = 'all-clear'
this.calculateButton.textContent = '='
this.calculateButton.value = '='
//using createElement('button','equal-sign operator') throws error
//because element.classList.add donot take spaces
//so usign className,' operator' is added to calculateButton
this.calculateButton.className += ' operator';
this.clearButton.className += ' operator';
this.keyScreen.append(this.addButton,
this.minusButton,
this.multiplyButton,
this.divideButton,
this.NineButton,
this.NineButton,
this.EightButton,
this.SevenButton,
this.SixButton,
this.FiveButton,
this.FourButton,
this.ThreeButton,
this.TwoButton,
this.OneButton,
this.ZeroButton,
this.dotButton,
this.clearButton,
this.calculateButton
)
this.title = this.createElement('h1')
this.title.textContent = 'Calculator'
this.calculator.append(this.screen,this.keyScreen)
this.app.append(this.title,this.calculator)
}
// Create an element with an optional CSS class
createElement(tag, className) {
const element = document.createElement(tag)
if (className) element.classList.add(className)
return element
}
// Retrieve an element from the DOM
getElement(selector) {
const element = document.querySelector(selector)
return element
}
updateDisplay(displayValue){
const display = document.querySelector('.calculator-screen');
display.value = displayValue;
}
bindUpdateDisplay(handler) {
this.keyScreen.addEventListener('click', (event) => {
const {target} = event;
handler(target);
});
}
}//View
/**
* @class Controller
*
* Links the user input and the view output.
*
* @param model
* @param view
*/
class Controller {
constructor(model, view) {
this.model = model
this.view = view
//Explicit this binding
this.view.bindUpdateDisplay(this.onClickChanged)
}
onClickChanged = target => {
this.model.handleValue(target)
this.view.updateDisplay(this.model.displayValue)
}
}
const app = new Controller(new Model(), new View())