Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
</body>
</html>
 
function Calc(){
  var res = 0;
  var called = '';
  function method(val) {
    return method[called](val);
  }
  method.add = function add(x) {
    if (!isNaN(x)) res += x;
    called = 'add';
    return this;
  };
  
  method.subtract = function(x) {
    if (!isNaN(x)) res -= x;
    called = 'subtract';
    return this;
  };
  method.divide = function(x) {
    if ((!isNaN(x)) && (x !== 0)) res /= x;
    called = 'divide';
    return this;
  };
  
  method.multiply = function(x) {
    if (!isNaN(x)) res *= x;
    called = 'multiply';
    return this;
  };
  method.reset = function() {
    res = 0;
    called = 'reset';
    return this;
  };
  method.getResult = function() {
    return res;
  };
  return method;
}
var Calculator = Calc();
const result = Calculator.add(100)(10)
    .multiply(2)(2)
    .divide(20)(2)
    .reset()
    .add(8)(4)(42)
    .subtract(1)(1)(10)
    .getResult();
console.log(result); // 42
Output 300px

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers