Add basic functions
This commit is contained in:
parent
7f0d5c8fff
commit
acc49ea098
1 changed files with 25 additions and 0 deletions
25
script.js
25
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);
|
||||
};
|
||||
}
|
||||
Loading…
Reference in a new issue