簡體   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