(我失败了)Javascript 挑战:带闭包的函数 add()()()

(My failed) Javascript challenge: function add()()() with closures

需要编写一个函数,最多可以获取 3 个参数和 return 个总和。 这是一种方法,如何调用:

add(2, 5, 10); // 17
add(2, 5)(10); // 17
add(2)(5)(10); // 17
add(2)(5, 10); // 17

我写了一个函数,可以做到:

function add(a) {
  var currentSum = [].reduce.call(arguments, function(c, d) { return c + d; });

  function f(b) {
    currentSum += [].reduce.call(arguments, function(c, d) { return c + d; });
    return f;
  }

  f.toString = function() {
    return currentSum;
  };

  return f;
}

但是!挑战任务说我不能使用 valueOf 的 toString 来获得结果。 我该如何解决?

P.S。我注意到我挑战失败了,所以我为什么要问。

下面的修复将解决这个问题

如下所示创建 "add" 函数,最多可处理 3 个参数

function add(a,b,c) {
  return (((a!=null && a>0)?a:0) + ((b!=null && b >0)?b:0 ) + ((c!=null && c >0)?c:0 ));
}

您只需要调用它并根据您的要求传递参数,例如

add(2);//will give 2 as result
add(2,3);//Will give 5 as result
add(2,3,4)//will give 9 as result

虽然如果你想让格式严格传递 3 个参数,那么你可以在参数中传递空值,即 add(2)add(2,null,null) 会提供相同的结果。 希望这能解决您的问题。

我认为你需要做的是,一旦你处理了 3 个参数,你将必须 return 总和,而不是函数

function add() {
  var sum = 0,
    counter = 0;

  function local() {
    [].some.call(arguments, function(value) {
      sum += value;
      return ++counter >= 3;
    })

    return counter < 3 ? local : sum;
  }

  return local.apply(this, arguments)
}

snippet.log(add(10, 5, 2));
snippet.log(add(10)(5, 2));
snippet.log(add(10)(5)(2));
snippet.log(add(10, 5)(2));
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

这是另一种基于检查传递的参数是否为 undefined 类型的方法。

function add(x, y, z) {
    // Both y, z not given
    if(typeof y === "undefined" &&
       typeof z === "undefined")
        return function (a, b) {
            // b not given
            if(typeof b === "undefined")
                return function (c) { return x + a + c;  };
             else 
                return x + a + b; 
        };
    // Only z not given
    if(!(typeof y === "undefined") && 
         typeof z === "undefined") 
        return function (d) { return x + y + d; };
    return x + y + z;
}

console.log("add(2)(3)(4) = " + add(2)(3)(4)); // 9
console.log("add(2)(3, 4) = " + add(2)(3, 4)); // 9
console.log("add(2, 3)(4) = " + add(2, 3)(4)); // 9
console.log("add(2, 3, 4) = " + add(2, 3, 4)); // 9

注意@Arun给出的答案更好更简洁

比较函数的数量 (add.length) 与实际参数数量,如果参数较少,return 闭包:

Array.from = Array.from || function(x) { return [].slice.call(x) };

function add(a, b, c) {
  
  var args = Array.from(arguments);
  
  if(args.length < add.length)
    return function() {
        return add.apply(null,
                         args.concat(Array.from(arguments)));
    }

  return args.reduce(function(x, y) { return x + y }, 0);
}

document.write("<pre>")
document.writeln(add(1)(2)(3));
document.writeln(add(1, 2)(3));
document.writeln(add(1)(2, 3));
document.writeln(add(1, 2, 3));

你可以这样做:

function ab(a,b,c){
   if(arguments.length==3){return(a+b+c)};
   if(arguments.length==2){return(function(c){return(a+b+c)})};
   if(arguments.length==1){
   return(function(b,c){ if(arguments.length==2) {return(a+b+c)}
   else{ return(function(c){console.log(a,b,c); return(a+b+c)})}
}
)} 
  } ab(1,67,9);