Skip to content

Commit 7f5c9f4

Browse files
committed
feat_dev: improve "inputValidation" function, line 62, 63
1 parent ee2a4bd commit 7f5c9f4

File tree

1 file changed

+4
-10
lines changed

1 file changed

+4
-10
lines changed

src/script.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
2823
function factorial(numb) {
2924
if (isNaN(numb)) throw new Error("Invalid factorial");
3025
let result = 1;
@@ -51,24 +46,23 @@ function handleParentheses(eval_str) {
5146

5247
function 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.]+)Math/g, '$1*Math.sqrt(');
6356
// Replace "log" and with "Math.log10("
6457
eval_str = eval_str.replace(/log/g, "Math.log10(");
6558
// Replace "mod" and with "%"
6659
eval_str = eval_str.replace(/mod/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.]+)Math/g, '$1*Math');
6964
// Auto close prantheses
7065
eval_str = handleParentheses(eval_str);
71-
7266
} catch {
7367
display.innerText = "Error";
7468
}

0 commit comments

Comments
 (0)