1+ {{ $formula := .Get "formula" | default "n1 * m1 + n2 - m2 / n3" }}
2+ {{ $n1 := .Get "n1" | default 1 }}
3+ {{ $m1 := .Get "m1" | default 4 }}
4+ {{ $n2 := .Get "n2" | default 2 }}
5+ {{ $m2 := .Get "m2" | default 5 }}
6+ {{ $n3 := .Get "n3" | default 3 }}
7+ {{ $m3 := .Get "m3" | default 6 }}
8+
9+
10+ < div id ="calculator-container ">
11+ < div >
12+ < label for ="n1 "> n1:</ label >
13+ < input type ="number " id ="n1 " value ="{{ $n1 }} ">
14+ </ div >
15+ < div >
16+ < label for ="m1 "> m1:</ label >
17+ < input type ="number " id ="m1 " value ="{{ $m1 }} ">
18+ </ div >
19+ < div >
20+ < label for ="n2 "> n2:</ label >
21+ < input type ="number " id ="n2 " value ="{{ $n2 }} ">
22+ </ div >
23+ < div >
24+ < label for ="m2 "> m2:</ label >
25+ < input type ="number " id ="m2 " value ="{{ $m2 }} ">
26+ </ div >
27+ < div >
28+ < label for ="n3 "> n3:</ label >
29+ < input type ="number " id ="n3 " value ="{{ $n3 }} ">
30+ </ div >
31+ < div >
32+ < label for ="m3 "> m3:</ label >
33+ < input type ="number " id ="m3 " value ="{{ $m3 }} ">
34+ </ div >
35+ < div >
36+ < label for ="formula "> Formula:</ label >
37+ {{ if eq $formula "" }}
38+ < input type ="text " id ="formula " value ="n1 * m1 + n2 - m2 / n3 ">
39+ {{ else }}
40+ < input type ="text " id ="formula " value ="{{ $formula }} ">
41+ {{ end }}
42+ </ div >
43+ < button onclick ="calculate() "> Solve</ button >
44+ < div id ="result "> Result: </ div >
45+ </ div >
46+
47+ < script >
48+ function calculate ( ) {
49+ const n1 = parseFloat ( document . getElementById ( 'n1' ) . value ) ;
50+ const m1 = parseFloat ( document . getElementById ( 'm1' ) . value ) ;
51+ const n2 = parseFloat ( document . getElementById ( 'n2' ) . value ) ;
52+ const m2 = parseFloat ( document . getElementById ( 'm2' ) . value ) ;
53+ const n3 = parseFloat ( document . getElementById ( 'n3' ) . value ) ;
54+ const m3 = parseFloat ( document . getElementById ( 'm3' ) . value ) ;
55+ const formula = document . getElementById ( 'formula' ) . value ;
56+ const resultDiv = document . getElementById ( 'result' ) ;
57+
58+ try {
59+ // Replace variable names in the formula string
60+ let expression = formula . replace ( / n 1 / g, n1 ) ;
61+ expression = expression . replace ( / m 1 / g, m1 ) ;
62+ expression = expression . replace ( / n 2 / g, n2 ) ;
63+ expression = expression . replace ( / m 2 / g, m2 ) ;
64+ expression = expression . replace ( / n 3 / g, n3 ) ;
65+ expression = expression . replace ( / m 3 / g, m3 ) ;
66+
67+ // Evaluate the expression using JavaScript's eval() function
68+ // WARNING: Using eval() can be risky with untrusted input.
69+ const finalAnswer = eval ( expression ) ;
70+ resultDiv . textContent = `Result: ${ finalAnswer } ` ;
71+ } catch ( error ) {
72+ resultDiv . textContent = `Result: Error in formula` ;
73+ console . error ( "Formula evaluation error:" , error ) ;
74+ }
75+ }
76+ </ script >
77+
78+ < style >
79+ # calculator-container {
80+ max-width : 350px ;
81+ margin : 2em auto;
82+ padding : 1.5em 2em ;
83+ border-radius : 12px ;
84+ background : # f9f9f9 ;
85+ box-shadow : 0 2px 12px rgba (0 , 0 , 0 , 0.08 );
86+ font-family : system-ui, sans-serif;
87+ transition : background 0.3s , color 0.3s ;
88+ }
89+
90+ # calculator-container div {
91+ margin-bottom : 1em ;
92+ display : flex;
93+ align-items : center;
94+ gap : 0.5em ;
95+ }
96+
97+ # calculator-container label {
98+ flex : 0 0 60px ;
99+ font-weight : 500 ;
100+ }
101+
102+ # calculator-container input [type = "number" ],
103+ # calculator-container input [type = "text" ] {
104+ flex : 1 ;
105+ padding : 0.4em 0.6em ;
106+ border : 1px solid # ccc ;
107+ border-radius : 6px ;
108+ font-size : 1em ;
109+ background : # fff ;
110+ color : # 222 ;
111+ transition : border 0.2s ;
112+ }
113+
114+ # calculator-container input : focus {
115+ border-color : # 0078d4 ;
116+ outline : none;
117+ }
118+
119+ # calculator-container button {
120+ width : 100% ;
121+ padding : 0.6em 0 ;
122+ background : # 0078d4 ;
123+ color : # fff ;
124+ border : none;
125+ border-radius : 6px ;
126+ font-size : 1.1em ;
127+ font-weight : 600 ;
128+ cursor : pointer;
129+ transition : background 0.2s ;
130+ }
131+
132+ # calculator-container button : hover {
133+ background : # 005fa3 ;
134+ }
135+
136+ # result {
137+ margin-top : 1em ;
138+ font-size : 1.1em ;
139+ font-weight : 500 ;
140+ color : # 007800 ;
141+ min-height : 1.5em ;
142+ }
143+
144+ @media (prefers-color-scheme : dark) {
145+ # calculator-container {
146+ background : # 23272f ;
147+ color : # f3f3f3 ;
148+ box-shadow : 0 2px 12px rgba (0 , 0 , 0 , 0.32 );
149+ }
150+ # calculator-container input [type = "number" ],
151+ # calculator-container input [type = "text" ] {
152+ background : # 181b20 ;
153+ color : # f3f3f3 ;
154+ border : 1px solid # 444 ;
155+ }
156+ # calculator-container input : focus {
157+ border-color : # 4fa3ff ;
158+ }
159+ # calculator-container button {
160+ background : # 4fa3ff ;
161+ color : # 181b20 ;
162+ }
163+ # calculator-container button : hover {
164+ background : # 0078d4 ;
165+ color : # fff ;
166+ }
167+ # result {
168+ color : # 7fff7f ;
169+ }
170+ }
171+ </ style >
0 commit comments