【问题标题】:(My failed) Javascript challenge: function add()()() with closures(我失败了)Javascript 挑战:带有闭包的函数 add()()()
【发布时间】:2015-08-28 06:42:44
【问题描述】:

需要编写一个函数,最多可以获取 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;
}

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

附:我注意到我挑战失败了,所以我为什么要问。

【问题讨论】:

  • 挑战是不是也说要自己解决?
  • 我已经注意到我自己失败了。这就是我问的原因。
  • 好的,我可以给出线索你现在可以返回函数试试
  • 但是我已经返回了一个函数...
  • 已经结束 =) 我告诉我的雇主我去 SO 寻求答案 =) 我猜这等于失败 =)

标签: javascript function closures


【解决方案1】:

我认为你需要做的是,一旦你处理了 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>

【讨论】:

  • 谢谢。我认为这是最明确和最相关的答案。和我想的很接近。
【解决方案2】:

将函数 (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】:

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

    【讨论】:

      【解决方案4】:

      以下修复将解决此问题

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

      【讨论】:

      • 他没有要求过滤空值。您的函数不能像add(2, 3)(5)add(2)(3)(5) 那样调用。他想要连锁电话。
      【解决方案5】:

      你可以这样做:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-04-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-21
        • 2017-12-21
        相关资源
        最近更新 更多