1
- $ ( document ) . ready ( function ( ) {
2
- var userInput = [ "" ] ;
3
- var subTotal ;
4
- var variable1 = [ "+" , "-" , "*" , "/" ] ;
5
- var variable2 = [ "." ] ;
6
- var numb = [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] ;
7
-
8
- function getValue ( input ) {
9
- if ( variable2 . includes ( userInput [ userInput . length - 1 ] ) === true && input === "." ) {
10
- console . log ( "Duplicate '.'" ) ;
11
- }
12
- else if ( userInput . length === 1 && variable1 . includes ( input ) === false ) {
13
- userInput . push ( input ) ;
14
- }
15
- else if ( variable1 . includes ( userInput [ userInput . length - 1 ] ) === false ) {
16
- userInput . push ( input ) ;
17
- }
18
- else if ( numb . includes ( Number ( input ) ) ) {
19
- userInput . push ( input ) ;
20
- }
21
- updated ( ) ;
1
+ // no need for jquery: it's a really big library: bad for performace
2
+
3
+ const $ = ( selector ) => {
4
+ return document . querySelector ( selector ) ;
5
+ }
6
+
7
+
8
+
9
+
10
+ class Calculator {
11
+ constructor ( ) {
12
+ this . state = {
13
+ monitor : [ ] ,
14
+ output : ""
15
+ } ;
16
+ this . output = document . getElementById ( "steps" ) ;
17
+ this . input = document . getElementById ( "calBody" ) ;
18
+ this . bound = this . bound . bind ( this ) ;
19
+ this . getUserInput ( ) ;
20
+ this . leaveZero ( ) ;
22
21
}
23
22
24
- function updated ( ) {
25
- subTotal = userInput . join ( "" ) ;
26
- $ ( "#steps" ) . html ( subTotal ) ;
23
+ render ( ) {
24
+ this . output . innerHTML = this . state . output ;
27
25
}
28
26
29
- function getTotal ( ) {
30
- subTotal = userInput . join ( "" ) ;
31
- console . log ( subTotal + ": " + eval ( subTotal ) ) ;
32
- $ ( "#steps" ) . html ( eval ( subTotal ) ) ;
27
+ getUserInput ( ) {
28
+ this . input . addEventListener ( 'click' , ( e ) => {
29
+ var id = e . target . id ;
30
+ if ( id !== "calBody" ) {
31
+ const ctrkeys = [ "deleteAll" , "backOne" , "total" ] ;
32
+ ctrkeys . includes ( id ) ? this . commandStation ( id ) : this . regularClick ( id ) ;
33
+ }
34
+ } ) ;
33
35
}
34
36
35
- $ ( "button" ) . on ( "click" , function ( ) {
36
- if ( this . id === "deleteAll" ) {
37
- userInput = [ "" ] ;
38
- updated ( ) ;
39
- }
40
- else if ( this . id === "backOne" ) {
41
- userInput . pop ( ) ;
42
- updated ( ) ;
43
- }
44
- else if ( this . id === "total" ) {
45
- getTotal ( ) ;
37
+
38
+
39
+ commandStation ( id ) {
40
+ switch ( id ) {
41
+ case "deleteAll" :
42
+ this . deleteAll ( ) ;
43
+ break ;
44
+ case "backOne" :
45
+ this . backOne ( ) ;
46
+ case "total" :
47
+ this . total ( ) ;
48
+ default :
49
+ break ;
46
50
}
47
- else {
48
- if ( userInput [ userInput . length - 1 ] . indexOf ( "+" , "-" , "/" , "*" , "." ) === - 1 ) {
49
- getValue ( this . id ) ;
50
- }
51
- else {
52
- getValue ( this . id ) ;
53
- }
51
+ }
52
+
53
+ regularClick ( id ) {
54
+ this . state . monitor . push ( id ) ;
55
+ this . state . output = this . state . monitor . join ( "" ) ;
56
+ this . render ( ) ;
57
+ console . log ( this . state . monitor ) ;
58
+ }
59
+
60
+ deleteAll ( ) {
61
+ this . state = {
62
+ monitor : [ ] ,
63
+ output : ""
54
64
}
55
- } ) ;
56
- function windowClose ( ) {
57
- window . open ( '' , '_parent' , '' ) ;
58
- window . close ( ) ;
65
+ this . render ( ) ;
66
+ this . leaveZero ( ) ;
67
+ }
68
+
69
+ backOne ( ) {
70
+ this . state . monitor . pop ( ) ;
71
+ this . state . output = this . state . monitor . join ( "" ) ;
72
+ this . render ( ) ;
73
+ console . log ( this . state )
74
+ }
75
+
76
+ total ( ) {
77
+ const total = eval ( this . state . monitor . join ( "" ) ) ;
78
+
79
+ this . state = {
80
+ output : total ,
81
+ monitor : [ ] . concat ( total )
82
+ } ;
83
+ this . render ( ) ;
59
84
}
60
- } ) ;
85
+
86
+
87
+
88
+ bound ( ) {
89
+ this . deleteAll = this . deleteAll . bind ( this ) ;
90
+ this . backOne = this . backOne . bind ( this ) ;
91
+ this . total = this . total . bind ( this ) ;
92
+ this . commandStation = this . commandStation . bind ( this ) ;
93
+ this . getUserInput = this . getUserInput . bind ( this ) ;
94
+ this . render = this . render . bind ( this ) ;
95
+ }
96
+ leaveZero ( ) {
97
+ this . output . innerHTML = "0" ;
98
+ }
99
+
100
+ }
101
+
102
+
103
+
104
+ const StartCalc = new Calculator ( ) ;
0 commit comments