[英](My failed) Javascript challenge: function add()()() with closures
Need to write a function, that can get up to 3 arguments and return a sum.需要编写一个函数,最多可以有 3 个参数并返回一个总和。 Here is a ways, how it can be called:
这是一种方法,如何调用它:
add(2, 5, 10); // 17
add(2, 5)(10); // 17
add(2)(5)(10); // 17
add(2)(5, 10); // 17
I have write a function, that can do it:我写了一个函数,可以做到:
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;
}
BUT!但! The challenge task says that I CAN'T use toString of valueOf to get result.
挑战任务说我不能使用 valueOf 的 toString 来获得结果。 How can I solve it?
我该如何解决?
PS I have noticed that I failed the challenge, so why I'm asking. PS 我注意到我在挑战中失败了,所以我为什么要问。
I think what you need to do is, once you have processed 3 parameters, you will have to return the sum, not the function我认为你需要做的是,一旦你处理了 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>
Compare the arity of the function ( add.length
) with the actual number of arguments and return a closure if there are less arguments:比较函数的数量(
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));
Below fix would resolve this下面的修复将解决这个问题
Create "add" function as below which would handle up to 3 arguments创建如下“添加”函数,最多可处理 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 ));
}
You just need to call it and pass arguments as per your requirement like您只需要根据您的要求调用它并传递参数,例如
add(2);//will give 2 as result
add(2,3);//Will give 5 as result
add(2,3,4)//will give 9 as result
Although if you want to make format strict to pass 3 arguments then you can pass null in arguments ie add(2)
and add(2,null,null)
would provide same result.虽然如果你想让格式严格传递 3 个参数,那么你可以在参数中传递 null,即
add(2)
和add(2,null,null)
将提供相同的结果。 Hope this would solve your issue.希望这能解决您的问题。
Here is another approach based on checking if the parameters passed are of type undefined
or not.这是另一种基于检查传递的参数是否为
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
Note that the answer given by @Arun is better and more concise.请注意,@Arun 给出的答案更好更简洁。
You can do something like this:你可以这样做:
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.