Skip to content

Instantly share code, notes, and snippets.

@colevandersWands
Created September 18, 2017 12:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colevandersWands/4ee9660433f2bdc096b13a4455c46d09 to your computer and use it in GitHub Desktop.
Save colevandersWands/4ee9660433f2bdc096b13a4455c46d09 to your computer and use it in GitHub Desktop.
calc object that runs in the terminal
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