From 5430fd7e73c0864dedbe271bab0d7146425be95b Mon Sep 17 00:00:00 2001 From: Chathura Dharmasiri Date: Sun, 25 May 2025 14:29:17 +0530 Subject: [PATCH 1/6] Create index.js --- index.js | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 index.js diff --git a/index.js b/index.js new file mode 100644 index 0000000..66bda0f --- /dev/null +++ b/index.js @@ -0,0 +1,2 @@ +//hello world +console.log("hello world") From 3ec297693d051656b1b2cab2f4adabd6e266a4fa Mon Sep 17 00:00:00 2001 From: Chathura Dharmasiri Date: Sun, 25 May 2025 14:31:53 +0530 Subject: [PATCH 2/6] Update index.js --- index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/index.js b/index.js index 66bda0f..abc60e0 100644 --- a/index.js +++ b/index.js @@ -1,2 +1,4 @@ //hello world console.log("hello world") +//adding +console.log("11" + "11") From 8051f50844a44e2dcc7d0ea1242a54927c52569e Mon Sep 17 00:00:00 2001 From: Chathura Dharmasiri Date: Sun, 25 May 2025 14:35:56 +0530 Subject: [PATCH 3/6] Update index.js --- index.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/index.js b/index.js index abc60e0..4751bf9 100644 --- a/index.js +++ b/index.js @@ -2,3 +2,10 @@ console.log("hello world") //adding console.log("11" + "11") + + +function add(a, b) { + return a + b; +} + +console.log(add(5, 3)); // Output: 8 From 1252c93e73f99d23dcd4284b48c0ab9f0b76fbef Mon Sep 17 00:00:00 2001 From: Chathura Dharmasiri Date: Sun, 25 May 2025 14:43:04 +0530 Subject: [PATCH 4/6] Update index.js --- index.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/index.js b/index.js index 4751bf9..1767329 100644 --- a/index.js +++ b/index.js @@ -9,3 +9,10 @@ function add(a, b) { } console.log(add(5, 3)); // Output: 8 + + +function add(a) { + return a +} + +console.log(add("hello")); From a40feeef63a195285901a22ba5d73beb643da9ec Mon Sep 17 00:00:00 2001 From: Chathura Dharmasiri Date: Sun, 25 May 2025 14:43:20 +0530 Subject: [PATCH 5/6] Update index.js --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 1767329..535a9ba 100644 --- a/index.js +++ b/index.js @@ -8,7 +8,7 @@ function add(a, b) { return a + b; } -console.log(add(5, 3)); // Output: 8 +console.log(add(5, 3)); function add(a) { From 9569ba116eb96c0ad89b73f23310ef299d00aa52 Mon Sep 17 00:00:00 2001 From: Chathura Dharmasiri Date: Sun, 25 May 2025 14:46:36 +0530 Subject: [PATCH 6/6] Update index.js --- index.js | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/index.js b/index.js index 535a9ba..3cdce3f 100644 --- a/index.js +++ b/index.js @@ -1,18 +1,27 @@ -//hello world -console.log("hello world") -//adding -console.log("11" + "11") +function advancedCalculator(expression) { + try { + // Clean up the expression: remove invalid characters + if (!/^[\d\s+\-*/%^().]+$/.test(expression)) { + return "Error: Invalid characters in expression."; + } + // Replace ^ with Math.pow-compatible syntax + expression = expression.replace(/(\d+)\s*\^\s*(\d+)/g, 'Math.pow($1, $2)'); -function add(a, b) { - return a + b; -} - -console.log(add(5, 3)); - + // Evaluate the expression using Function constructor + const result = Function('"use strict"; return (' + expression + ')')(); -function add(a) { - return a + return Number.isFinite(result) ? result : "Error: Invalid calculation"; + } catch (error) { + return "Error: Invalid expression"; + } } -console.log(add("hello")); +// Example usage: +console.log(advancedCalculator("3 + 5 * 2")); // 13 +console.log(advancedCalculator("10 / 2 + 6")); // 11 +console.log(advancedCalculator("2 ^ 3 + 1")); // 9 +console.log(advancedCalculator("(5 + 3) * 2")); // 16 +console.log(advancedCalculator("10 % 3 + 4")); // 5 +console.log(advancedCalculator("10 / (5 - 5)")); // Error: Invalid calculation +console.log(advancedCalculator("2 + bad_input")); // Error: Invalid characters in expression.