@@ -20,11 +20,6 @@ function backspace() {
2020 display . innerText = display . innerText . slice ( 0 , - 1 ) || "0" ;
2121}
2222
23- function pi ( eval_str ) {
24- // Replace "π" with "Math.PI"
25- return eval_str = eval_str . replace ( / π / g, "Math.PI" ) ;
26- }
27-
2823function factorial ( numb ) {
2924 if ( isNaN ( numb ) ) throw new Error ( "Invalid factorial" ) ;
3025 let result = 1 ;
@@ -51,24 +46,23 @@ function handleParentheses(eval_str) {
5146
5247function inputValidation ( eval_str ) {
5348 try {
49+ // Replace "π" with "(3.142)"
50+ eval_str = eval_str . replace ( / π / g, "(3.142)" ) ;
5451 // Add "*" before or after prantheses that haven't any operators
5552 eval_str = eval_str . replace ( / ( [ \d . ] + ) \( / g, '$1*(' ) ;
5653 eval_str = eval_str . replace ( / \) (? = [ \d . ] ) / g, ')*' ) ;
57- // Replace "π" with "Math.PI"
58- eval_str = pi ( eval_str )
5954 // Replace "√" with "Math.sqrt(" in "eval_str"
6055 eval_str = eval_str . replace ( / √ / g, "Math.sqrt" ) ;
61- // Add "*" before "√", handles cases like 2√16
62- eval_str = eval_str . replace ( / ( [ \d . ] + ) M a t h / g, '$1*Math.sqrt(' ) ;
6356 // Replace "log" and with "Math.log10("
6457 eval_str = eval_str . replace ( / l o g / g, "Math.log10(" ) ;
6558 // Replace "mod" and with "%"
6659 eval_str = eval_str . replace ( / m o d / g, "%" ) ;
6760 // Replace factorial (simple integer case)
6861 eval_str = eval_str . replace ( / ( \d + ) ! / g, 'factorial($1)' ) ;
62+ // If there was a number before the operator, add "*" before "Math.", handles cases like 2√(16)
63+ eval_str = eval_str . replace ( / ( [ \d . ] + ) M a t h / g, '$1*Math' ) ;
6964 // Auto close prantheses
7065 eval_str = handleParentheses ( eval_str ) ;
71-
7266 } catch {
7367 display . innerText = "Error" ;
7468 }
0 commit comments