-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
75 lines (61 loc) · 1.94 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
63
64
65
66
67
68
69
70
71
72
73
74
75
class Calculator {
constructor() { // Setup calculator
this.reset();
}
addNumber(n) { // Add number or digit
if (typeof n !== 'number' || isNaN(n)) {
throw new TypeError('You can add only numbers');
}
if (this.operator && this.operationStack.length > 0) {
this.operationStack.push(this.operator);
this.operator = null;
}
this.currentNumber = Number(`${this.currentNumber}${n}`);
}
addOperator(o) { // Add operator (+, -, * or /)
if (typeof o !== 'string' || ['+', '-', '*', '/'].indexOf(o) < 0) {
throw new TypeError('Operator must be +, -, * or /');
}
this.operator = o;
this.operationStack.push(this.currentNumber);
this.currentNumber = 0;
}
equals() { // Calculate current total
if (typeof this.currentNumber === 'number' && !isNaN(this.currentNumber)) {
this.operationStack.push(this.currentNumber);
this.currentNumber = 0;
}
let result = this.operationStack.slice(0);
let total = result.shift();
for (let i = 0; i < result.length; i += 2) {
total = this._executeOperation(total, result[i], result[i + 1]);
}
this.currentNumber = total;
console.log(this.operationStack);
console.log(total);
}
_executeOperation(left, operator, right) {
if (operator === '+') {
return left += right;
} else if (operator === '-') {
return left -= right;
} else if (operator === '/') {
return left /= right;
} else if (operator === '*') {
return left *= right;
}
}
reset() { // Reset all fields
this.currentNumber = 0;
this.operator = null;
this.operationStack = [];
}
}
const calculator = new Calculator();
const button = document.getElementById('button1');
button.addEventListener('click', function (event) {
console.log('Click');
console.log(event.target.value);
calculator.addNumber(Number(event.target.value));
calculator.equals();
});