|
1 |
| -class Calculator { |
2 |
| - constructor(previousOperandTextElement, currentOperandTextElement) { |
3 |
| - this.previousOperandTextElement = previousOperandTextElement |
4 |
| - this.currentOperandTextElement = currentOperandTextElement |
5 |
| - this.clear() |
6 |
| - } |
7 |
| - |
8 |
| - clear() { |
9 |
| - this.currentOperand = "" |
10 |
| - this.previousOperand = "" |
11 |
| - this.operation = undefined |
12 |
| - } |
13 |
| - |
14 |
| - delete() { |
15 |
| - this.currentOperand = this.currentOperand.toString().slice(0, -1) |
16 |
| - |
17 |
| - } |
18 |
| - |
19 |
| - appendNumber(number) { |
20 |
| - if (number === "." && this.currentOperand.includes(".")) return |
21 |
| - this.currentOperand = this.currentOperand.toString() + number.toString() |
22 |
| - } |
23 |
| - |
24 |
| - chooseOperation(operation) { |
25 |
| - if (this.currentOperand === "") return |
26 |
| - if (this.previousOperand !== "") { |
27 |
| - this.compute() |
28 |
| - } |
29 |
| - this.operation = operation |
30 |
| - this.previousOperand = this.currentOperand |
31 |
| - this.currentOperand = "" |
32 |
| - } |
33 |
| - |
34 |
| - compute() { |
35 |
| - let computation |
36 |
| - const prev = parseFloat(this.previousOperand) |
37 |
| - const curr = parseFloat(this.currentOperand) |
38 |
| - if (isNaN(prev) || isNaN(curr)) return |
39 |
| - switch (this.operation) { |
40 |
| - case "+": |
41 |
| - computation = prev + curr |
42 |
| - break; |
43 |
| - case "-": |
44 |
| - computation = prev - curr |
45 |
| - break; |
46 |
| - case "×": |
47 |
| - computation = prev * curr |
48 |
| - break; |
49 |
| - case "÷": |
50 |
| - computation = prev / curr |
51 |
| - break; |
52 |
| - default: |
53 |
| - return |
54 |
| - } |
55 |
| - this.currentOperand = computation |
56 |
| - this.operation = undefined |
57 |
| - this.previousOperand = "" |
58 |
| - |
59 |
| - } |
60 |
| - |
61 |
| - getDisplayNumber(number) { |
62 |
| - const stringNumber = number.toString() |
63 |
| - const integerDigits = parseFloat(stringNumber.split(".")[0]) |
64 |
| - const decimalDigits = stringNumber.split(".")[1] |
65 |
| - let integerDisplay |
66 |
| - if (isNaN(integerDigits)) { |
67 |
| - integerDisplay = "" |
68 |
| - } else { |
69 |
| - integerDisplay = integerDigits.toLocaleString("en", { maximumFractionDigits: 0 }) |
70 |
| - } |
71 |
| - if (decimalDigits != null) { |
72 |
| - return `${integerDisplay}.${decimalDigits}` |
73 |
| - } else { |
74 |
| - return integerDisplay |
75 |
| - } |
76 |
| - } |
77 |
| - |
78 |
| - updateDisplay() { |
79 |
| - this.currentOperandTextElement.innerText = this.getDisplayNumber(this.currentOperand) |
80 |
| - if (this.operation != null) { |
81 |
| - this.previousOperandTextElement.innerText = `${this.getDisplayNumber(this.previousOperand)} ${this.operation}` |
82 |
| - } else { |
83 |
| - this.previousOperandTextElement.innerText = "" |
84 |
| - } |
85 |
| - } |
86 |
| -} |
87 |
| - |
88 |
| -const numberButtons = document.querySelectorAll('[data-number]'); |
89 |
| -const operationButtons = document.querySelectorAll('[data-operation]'); |
90 |
| -const equalsButton = document.querySelector('[data-equals]'); |
91 |
| -const deleteButton = document.querySelector('[data-delete]'); |
92 |
| -const allClearButton = document.querySelector('[data-all-clear]'); |
93 |
| -const previousOperandTextElement = document.querySelector('[data-previous-operand]'); |
94 |
| -const currentOperandTextElement = document.querySelector('[data-current-operand]'); |
95 |
| - |
96 |
| -const calculator = new Calculator(previousOperandTextElement, currentOperandTextElement) |
97 |
| - |
98 |
| -numberButtons.forEach(button => { |
99 |
| - button.addEventListener("click", () => { |
100 |
| - calculator.appendNumber(button.innerText) |
101 |
| - calculator.updateDisplay() |
102 |
| - }) |
103 |
| -}) |
104 |
| -operationButtons.forEach(button => { |
105 |
| - button.addEventListener("click", () => { |
106 |
| - calculator.chooseOperation(button.innerText) |
107 |
| - calculator.updateDisplay() |
108 |
| - }) |
109 |
| -}) |
110 |
| -equalsButton.addEventListener("click", button => { |
111 |
| - calculator.compute() |
112 |
| - calculator.updateDisplay() |
113 |
| -}) |
114 |
| -allClearButton.addEventListener("click", button => { |
115 |
| - calculator.clear() |
116 |
| - calculator.updateDisplay() |
117 |
| -}) |
118 |
| -deleteButton.addEventListener("click", button => { |
119 |
| - calculator.delete() |
120 |
| - calculator.updateDisplay() |
121 |
| -}) |
122 |
| -
|
| 1 | +class Calculator { |
| 2 | + constructor(previousOperandTextElement, currentOperandTextElement) { |
| 3 | + this.previousOperandTextElement = previousOperandTextElement |
| 4 | + this.currentOperandTextElement = currentOperandTextElement |
| 5 | + this.clear() |
| 6 | + } |
| 7 | + |
| 8 | + clear() { |
| 9 | + this.currentOperand = "" |
| 10 | + this.previousOperand = "" |
| 11 | + this.operation = undefined |
| 12 | + } |
| 13 | + |
| 14 | + delete() { |
| 15 | + this.currentOperand = this.currentOperand.toString().slice(0, -1) |
| 16 | + |
| 17 | + } |
| 18 | + |
| 19 | + appendNumber(number) { |
| 20 | + if (number === "." && this.currentOperand.includes(".")) return |
| 21 | + this.currentOperand = this.currentOperand.toString() + number.toString() |
| 22 | + } |
| 23 | + |
| 24 | + chooseOperation(operation) { |
| 25 | + if (this.currentOperand === "") return |
| 26 | + if (this.previousOperand !== "") { |
| 27 | + this.compute() |
| 28 | + } |
| 29 | + this.operation = operation |
| 30 | + this.previousOperand = this.currentOperand |
| 31 | + this.currentOperand = "" |
| 32 | + } |
| 33 | + |
| 34 | + compute() { |
| 35 | + let computation |
| 36 | + const prev = parseFloat(this.previousOperand) |
| 37 | + const curr = parseFloat(this.currentOperand) |
| 38 | + if (isNaN(prev) || isNaN(curr)) return |
| 39 | + switch (this.operation) { |
| 40 | + case "+": |
| 41 | + computation = prev + curr |
| 42 | + break; |
| 43 | + case "-": |
| 44 | + computation = prev - curr |
| 45 | + break; |
| 46 | + case "×": |
| 47 | + computation = prev * curr |
| 48 | + break; |
| 49 | + case "÷": |
| 50 | + computation = prev / curr |
| 51 | + break; |
| 52 | + default: |
| 53 | + return |
| 54 | + } |
| 55 | + this.currentOperand = computation |
| 56 | + this.operation = undefined |
| 57 | + this.previousOperand = "" |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | + getDisplayNumber(number) { |
| 62 | + const stringNumber = number.toString() |
| 63 | + const integerDigits = parseFloat(stringNumber.split(".")[0]) |
| 64 | + const decimalDigits = stringNumber.split(".")[1] |
| 65 | + let integerDisplay |
| 66 | + if (isNaN(integerDigits)) { |
| 67 | + integerDisplay = "" |
| 68 | + } else { |
| 69 | + integerDisplay = integerDigits.toLocaleString("en", { maximumFractionDigits: 0 }) |
| 70 | + } |
| 71 | + if (decimalDigits != null) { |
| 72 | + return `${integerDisplay}.${decimalDigits}` |
| 73 | + } else { |
| 74 | + return integerDisplay |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + updateDisplay() { |
| 79 | + this.currentOperandTextElement.innerText = this.getDisplayNumber(this.currentOperand) |
| 80 | + if (this.operation != null) { |
| 81 | + this.previousOperandTextElement.innerText = `${this.getDisplayNumber(this.previousOperand)} ${this.operation}` |
| 82 | + } else { |
| 83 | + this.previousOperandTextElement.innerText = "" |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +const numberButtons = document.querySelectorAll('[data-number]'); |
| 89 | +const operationButtons = document.querySelectorAll('[data-operation]'); |
| 90 | +const equalsButton = document.querySelector('[data-equals]'); |
| 91 | +const deleteButton = document.querySelector('[data-delete]'); |
| 92 | +const allClearButton = document.querySelector('[data-all-clear]'); |
| 93 | +const previousOperandTextElement = document.querySelector('[data-previous-operand]'); |
| 94 | +const currentOperandTextElement = document.querySelector('[data-current-operand]'); |
| 95 | + |
| 96 | +const calculator = new Calculator(previousOperandTextElement, currentOperandTextElement) |
| 97 | + |
| 98 | +numberButtons.forEach(button => { |
| 99 | + button.addEventListener("click", () => { |
| 100 | + calculator.appendNumber(button.innerText) |
| 101 | + calculator.updateDisplay() |
| 102 | + }) |
| 103 | +}) |
| 104 | +operationButtons.forEach(button => { |
| 105 | + button.addEventListener("click", () => { |
| 106 | + calculator.chooseOperation(button.innerText) |
| 107 | + calculator.updateDisplay() |
| 108 | + }) |
| 109 | +}) |
| 110 | +equalsButton.addEventListener("click", button => { |
| 111 | + calculator.compute() |
| 112 | + calculator.updateDisplay() |
| 113 | +}) |
| 114 | +allClearButton.addEventListener("click", button => { |
| 115 | + calculator.clear() |
| 116 | + calculator.updateDisplay() |
| 117 | +}) |
| 118 | +deleteButton.addEventListener("click", button => { |
| 119 | + calculator.delete() |
| 120 | + calculator.updateDisplay() |
| 121 | +}) |
| 122 | + |
0 commit comments