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