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

(我的失败)Javascript挑战:带有闭包的函数add(),javascript,function,closures,Javascript,Function,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; }); funct

需要编写一个函数,它最多可以得到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的valueOf来获得结果。 我怎样才能解决它


另外,我注意到我没有通过挑战,所以我为什么要问这个问题。

下面的修复程序可以解决这个问题

创建如下所示的“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”函数,该函数最多可处理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)
将提供相同的结果。
希望这能解决您的问题。

我认为您需要做的是,一旦处理了3个参数,就必须返回总和,而不是函数

函数添加(){
var总和=0,
计数器=0;
函数局部(){
[].some.call(参数、函数(值){
总和+=数值;
返回++计数器>=3;
})
返回计数器<3?本地:总和;
}
返回local.apply(此,参数)
}
log(添加(10,5,2));
log(添加(10)(5,2));
日志(添加(10)(5)(2));
log(添加(10,5)(2))

我认为您需要做的是,一旦处理了3个参数,您就必须返回总和,而不是函数

函数添加(){
var总和=0,
计数器=0;
函数局部(){
[].some.call(参数、函数(值){
总和+=数值;
返回++计数器>=3;
})
返回计数器<3?本地:总和;
}
返回local.apply(此,参数)
}
log(添加(10,5,2));
log(添加(10)(5,2));
日志(添加(10)(5)(2));
log(添加(10,5)(2))

这里是另一种基于检查传递的参数是否为未定义类型的方法

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 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给出的答案更好、更简洁。

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

Array.from=Array.from | |函数(x){return[].slice.call(x)};
功能添加(a、b、c){
var args=Array.from(参数);
if(args.lengthfunction 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);
书面文件(增补第(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);
书面文件(增补(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);
书面文件(增补第(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);

书面文件(添加(1、2、3))将函数的arity(
add.length
)与实际参数数量进行比较,如果参数较少,则返回一个闭包:

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);
Array.from=Array.from | |函数(x){return[].slice.call(x)};
功能添加(a、b、c){
var args=Array.from(参数);
if(args.lengthfunction 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);
书面文件(增补第(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);
书面文件(增补(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);
书面文件(增补第(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);

书面文件(添加(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);

您可以这样做:


挑战是否也要求你自己解决?我已经注意到我自己失败了。这就是我问的原因。好吧,我可以给出你可以返回函数的线索,现在试试,但我已经返回了一个函数…它已经结束了=)我告诉我的雇主我去了,所以答案=)它等于失败,我想=)挑战也说你应该自己解决它吗?我已经注意到我自己失败了。这就是我问的原因。好的,我可以给你们线索,你们可以返回函数,现在试试,但我已经返回了一个函数…它已经结束了=)我告诉我的雇主我去了,所以答案=)它等于失败,我猜=)他不是要求过滤空值。不能像
add(2,3)(5)
add(2)(3)(5)
那样调用函数。他想要链式调用。他不是要求过滤空值。不能像
add(2,3)(5)
add(2)(3)(5)
那样调用函数。他要连环电话,谢谢。我认为这是最清楚和相关的答案。非常接近我的想法。谢谢。我认为这是最清楚和相关的答案。这与我的想法非常接近。