From acc49ea0981f5d2e5ba744a901524fbb2561d8a6 Mon Sep 17 00:00:00 2001 From: lolabird Date: Tue, 26 Dec 2023 17:15:28 -0700 Subject: [PATCH] Add basic functions --- script.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/script.js b/script.js index e69de29..51eb95e 100644 --- a/script.js +++ b/script.js @@ -0,0 +1,25 @@ +function Calculator(str) { + + this.methods = { + "-": (a, b) => (a - b), + "+": (a, b) => (a + b), + "/": (a, b) => (a / b), + "*": (a, b) => (a * b) + }; + + this.calculate = function(str) { + let parts = str.split(" "); + a = +parts[0]; + op = parts[1]; + b = +parts[2]; + + if (isNaN(a) || isNaN(b)) { + return NaN; + } + if (!this.methods[op]) { + return "Operation not supported." + } + + return this.methods[op](a, b); + }; +} \ No newline at end of file