2015-08-28 10 views
1

需要編寫一個函數,它可以得到3個參數並返回一個和。 這是一個方面,如何可以稱爲:(我的失敗)Javascript挑戰:函數add()()()與閉包

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

BUT!挑戰任務說我不能使用valueOf的toString來獲得結果。 我該如何解決它?

P.S.我注意到我沒有接受這個挑戰,所以爲什麼我要問。

+5

請問* *挑戰也說你應該自己解決問題? – Phil

+0

我已經注意到我自己失敗了。這就是我問的原因。 – Roman

+0

好吧,我可以給線索可以返回函數現在嘗試 –

回答

4

我認爲你需要做的是,一旦你已經處理了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>

+0

謝謝。我認爲這是最清楚和相關的答案。這與我的想法非常接近。 – Roman

0

下面的解決將解決此

創建「添加」功能,如下這將處理高達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)會提供相同的結果。 希望這可以解決你的問題。

+0

他沒有要求過濾空值。你的函數不能被稱爲'add(2,3)(5)'或'add(2)(3)(5)'。他想要連鎖電話。 –

0

這是基於另一種方法檢查傳遞的參數是否爲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給出的答案更好,更簡潔。

1

比較功能(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));

0

你可以做這樣的事情:

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