File tree Expand file tree Collapse file tree 1 file changed +21
-1
lines changed
days/025-028-javascript/demo/calculator Expand file tree Collapse file tree 1 file changed +21
-1
lines changed Original file line number Diff line number Diff line change @@ -38,7 +38,27 @@ <h1>Basic JS Calculator</h1>
38
38
39
39
< script type ="text/javascript ">
40
40
function doCalculation ( ) {
41
- // you code
41
+ let num1 = parseInt ( document . getElementById ( 'num1' ) . value ) ;
42
+ let num2 = parseInt ( document . getElementById ( 'num2' ) . value ) ;
43
+ let select = document . getElementById ( 'operator' ) ;
44
+ let operator = select . selectedOptions [ 0 ] . getAttribute ( 'name' ) ;
45
+
46
+ math = {
47
+ '+' : function ( x , y ) { return x + y } ,
48
+ '-' : function ( x , y ) { return x - y } ,
49
+ '*' : function ( x , y ) { return x * y } ,
50
+ '/' : function ( x , y ) { return x / y } ,
51
+ } ;
52
+
53
+ let result = math [ operator ] ( num1 , num2 ) ;
54
+ let result_element = document . getElementById ( 'result' ) ;
55
+
56
+ if ( isNaN ( result ) || ! isFinite ( result ) ) {
57
+ result_element . style . color = 'red' ;
58
+ } else {
59
+ result_element . style . color = 'green' ;
60
+ }
61
+ result_element . innerHTML = result ;
42
62
}
43
63
</ script >
44
64
You can’t perform that action at this time.
0 commit comments