1+ class Calculator {
2+ constructor ( currentOperandTextElement , previousOperandTextElement ) {
3+ this . currentOperandTextElement = currentOperandTextElement
4+ this . previousOperandTextElement = previousOperandTextElement
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+ appendNumber ( number ) {
19+ if ( number === '.' && this . currentOperand . includes ( '.' ) ) return
20+ this . currentOperand = this . currentOperand . toString ( ) + number . toString ( )
21+ }
22+
23+ chooseOperation ( operation ) {
24+ if ( this . currentOperand === '' ) return
25+ if ( this . previousOperand !== '' ) {
26+ this . compute ( )
27+ }
28+ this . operation = operation
29+ this . previousOperand = this . currentOperand
30+ this . currentOperand = ''
31+ }
32+
33+ updateDisplay ( ) {
34+ this . currentOperandTextElement . innerText = this . currentOperand . toLocaleString ( 'en' )
35+ if ( this . operation != null ) {
36+ this . previousOperandTextElement . innerText =
37+ `${ this . previousOperand . toLocaleString ( 'en' ) } ${ this . operation } `
38+ } else {
39+ this . previousOperandTextElement . innerText = null
40+ }
41+ }
42+
43+ compute ( ) {
44+ let computation
45+ const prev = parseFloat ( this . previousOperand )
46+ const current = parseFloat ( this . currentOperand )
47+ switch ( this . operation ) {
48+ case '+' :
49+ computation = prev + current
50+ break
51+ case '-' :
52+ computation = prev - current
53+ break
54+ case '*' :
55+ computation = prev * current
56+ break
57+ case '÷' :
58+ computation = prev / current
59+ break
60+ default :
61+ return
62+ }
63+ this . currentOperand = computation
64+ this . operation = undefined
65+ this . previousOperand = ''
66+ }
67+ }
68+
69+ const currentOperandTextElement = document . querySelector ( '[data-current-operand]' )
70+ const previousOperandTextElement = document . querySelector ( '[data-previous-operand]' )
71+ const calculator = new Calculator ( currentOperandTextElement , previousOperandTextElement )
72+
73+ const numberButtons = document . querySelectorAll ( '[data-number]' )
74+ const operationButton = document . querySelectorAll ( '[data-operation]' )
75+ const equalsButton = document . querySelector ( '[data-equals]' )
76+ const deleteButton = document . querySelector ( '[data-delete]' )
77+ const allClearButton = document . querySelector ( '[data-all-clear]' )
78+
79+ numberButtons . forEach ( button => {
80+ button . addEventListener ( 'click' , ( ) => {
81+ calculator . appendNumber ( button . innerText )
82+ calculator . updateDisplay ( )
83+ } )
84+ } )
85+
86+ operationButton . forEach ( button => {
87+ button . addEventListener ( 'click' , ( ) => {
88+ calculator . chooseOperation ( button . innerText )
89+ calculator . updateDisplay ( )
90+ } )
91+ } )
92+
93+ equalsButton . addEventListener ( 'click' , ( ) => {
94+ calculator . compute ( )
95+ calculator . updateDisplay ( )
96+ } )
97+
98+ allClearButton . addEventListener ( 'click' , ( ) => {
99+ calculator . clear ( )
100+ calculator . updateDisplay ( )
101+ } )
102+
103+ deleteButton . addEventListener ( 'click' , ( ) => {
104+ calculator . delete ( )
105+ calculator . updateDisplay ( )
106+ } )
0 commit comments