Add basic functions

This commit is contained in:
lolabird 2023-12-26 17:15:28 -07:00
commit acc49ea098

View file

@ -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);
};
}