1
1
let lastOperand = 0 ;
2
+ let repeatOperand = 0 ;
2
3
let operation = null ;
4
+ let lastIsOp = false ;
3
5
4
- const inputWindow = document . getElementById ( ' inputWindow' ) ;
6
+ const inputWindow = document . querySelector ( "# inputWindow" ) ;
5
7
8
+ document . querySelectorAll ( ".btn_num" ) . forEach ( element => {
9
+ element . addEventListener ( "click" , function ( ) {
10
+ if ( lastIsOp )
11
+ {
12
+ inputWindow . value = "" ;
13
+ lastIsOp = false ;
14
+ }
15
+ if ( inputWindow . value == 0 ) inputWindow . value = "" ;
16
+ inputWindow . value += this . textContent ;
17
+ } ) ;
18
+ } ) ;
6
19
7
- document . getElementById ( 'btn_clr' ) . addEventListener ( 'click' , function ( ) {
20
+ document . querySelectorAll ( ".btn_op" ) . forEach ( element => {
21
+ element . addEventListener ( "click" , function ( ) {
22
+ repeatOperand = 0 ;
23
+ } ) ;
24
+ } ) ;
25
+
26
+ document . querySelector ( "#btn_sum" ) . addEventListener ( "click" , function ( ) {
27
+ operation = "sum" ;
28
+ if ( ! lastIsOp )
29
+ {
30
+ inputWindow . value = parseInt ( inputWindow . value ) + lastOperand ;
31
+ lastOperand = parseInt ( inputWindow . value ) ;
32
+ }
33
+ lastIsOp = true ;
34
+ } ) ;
35
+
36
+ document . querySelector ( "#btn_sub" ) . addEventListener ( "click" , function ( ) {
37
+ operation = "sub" ;
38
+ if ( ! lastIsOp )
39
+ {
40
+ inputWindow . value = parseInt ( inputWindow . value ) - lastOperand ;
41
+ lastOperand = parseInt ( inputWindow . value ) ;
42
+ }
43
+ lastIsOp = true ;
44
+ } ) ;
45
+
46
+ document . querySelector ( "#btn_calc" ) . addEventListener ( "click" , function ( ) {
47
+ if ( ! lastIsOp )
48
+ {
49
+ repeatOperand = parseInt ( inputWindow . value ) ;
50
+ }
51
+ if ( operation === "sum" ) inputWindow . value = lastOperand + repeatOperand ;
52
+ else if ( operation === "sub" ) inputWindow . value = lastOperand - repeatOperand ;
53
+ lastOperand = parseInt ( inputWindow . value ) ;
54
+
55
+ lastIsOp = true ;
56
+ } ) ;
57
+
58
+ document . querySelector ( "#btn_clr" ) . addEventListener ( "click" , function ( ) {
8
59
lastOperand = 0 ;
60
+ repeatOperand = 0 ;
9
61
operation = null ;
10
- inputWindow . value = '' ;
11
- } )
12
-
62
+ lastIsOp = false ;
63
+ inputWindow . value = "0" ;
64
+ } ) ;
0 commit comments