繁体   English   中英

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

[英](My failed) Javascript challenge: function add()()() with closures

需要编写一个函数,最多可以有 3 个参数并返回一个总和。 这是一种方法,如何调用它:

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 来获得结果。 我该如何解决?

PS 我注意到我在挑战中失败了,所以我为什么要问。

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

 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>

比较函数的数量( add.length )与实际参数数量,如果参数较少则返回一个闭包:

 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));

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

创建如下“添加”函数,最多可处理 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 个参数,那么你可以在参数中传递 null,即add(2)add(2,null,null)将提供相同的结果。 希望这能解决您的问题。

这是另一种基于检查传递的参数是否为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 给出的答案更好更简洁。

你可以这样做:

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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM