Created
September 18, 2017 12:50
-
-
Save colevandersWands/4ee9660433f2bdc096b13a4455c46d09 to your computer and use it in GitHub Desktop.
calc object that runs in the terminal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var calc = { | |
lastResult: 0000, | |
operate: function(operation, arg1, arg2) { | |
switch(operation){ | |
case "add": | |
if(arg2){ | |
calc.lastResult = calc.add(arg1, arg2); | |
return this.lastResult; | |
}else{ | |
calc.lastResult = calc.add(arg1, this.lastResult); | |
return this.lastResult; | |
} | |
break; | |
case "subtract": | |
if(arg2){ | |
calc.lastResult = calc.subtract(arg1, arg2); | |
return this.lastResult; | |
}else{ | |
calc.lastResult = calc.subtract(arg1, this.lastResult); | |
return this.lastResult; | |
} | |
break; | |
case "multiply": | |
if(arg2){ | |
calc.lastResult = calc.multiply(arg1, arg2); | |
return this.lastResult; | |
}else{ | |
calc.lastResult = calc.multiply(arg1, this.lastResult); | |
return this.lastResult; | |
} | |
break; | |
case "divide": | |
if(arg2){ | |
calc.lastResult = calc.divide(arg1, arg2); | |
return this.lastResult; | |
}else{ | |
calc.lastResult = calc.divide(arg1, this.lastResult); | |
return this.lastResult; | |
} | |
break; | |
} | |
}, | |
add: function(arg1, arg2) { | |
return arg1 + arg2; | |
}, | |
subtract:function(arg1, arg2) { | |
return arg1 - arg2; | |
}, | |
multiply:function(arg1, arg2) { | |
return arg1 * arg2; | |
}, | |
divide:function(arg1, arg2) { | |
if(arg2===0||arg1===0){return 0} | |
else{return arg1 / arg2;} | |
} | |
}; | |
calc.clear = function() { | |
this.operate('multiply', 0); | |
} | |
var operation = process.argv[2]; | |
var a = parseInt(process.argv[3]); | |
var b = parseInt(process.argv[4]); | |
console.log(calc.operate(operation, a, b)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment