Skip to content

Instantly share code, notes, and snippets.

@Belrestro
Last active April 12, 2019 09:22
Show Gist options
  • Save Belrestro/0f30922206ccace130c0 to your computer and use it in GitHub Desktop.
Save Belrestro/0f30922206ccace130c0 to your computer and use it in GitHub Desktop.
var simMath = {
memo:{},
operation:function(array, method){
return Array.prototype.join.call(array, method);
},
calculate:function(array, method){
return Array.prototype.reduce.call(array, method);
},
plus: function(){
var operation = this.operation(arguments, '+');
if(operation in this.memo)
return this.memo[operation];
var result = this.calculate(arguments, function(a,b){return a+b});
this.memo[operation] = Number(result);
return result;
},
minus: function(){
var operation = this.operation(arguments, '-');
if(operation in this.memo)
return this.memo[operation];
var result = this.calculate(arguments, function(a,b){return a-b});
this.memo[operation] = Number(result);
return result;
},
multiply: function(){
var operation = this.operation(arguments, '*');
if(operation in this.memo)
return this.memo[operation];
var result = this.calculate(arguments, function(a,b){return a*b});
this.memo[operation] = Number(result);
return result;
},
devide: function(){
var operation = this.operation(arguments, '/');
if(operation in this.memo)
return this.memo[operation];
var result = this.calculate(arguments, function(a,b){return a/b});
this.memo[operation] = Number(result);
return result;
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment