How to curry a function across an unknown number of parameters

后端 未结 5 2085
灰色年华
灰色年华 2020-12-06 18:40

Say I have a function called multiplyDivide

If I were to call multiplyDivide(2)(3)(4)(6) it would be equivalent to 2 * 3 / 4 * 6

5条回答
  •  借酒劲吻你
    2020-12-06 19:05

    function addValues(a, b) {
       if(b!=undefined)
        return a + b;
    
       return function(b) {
           return a + b;
       }
    }
    let output1 = addValues(2)(4);  // 6
    let output2 = addValues(2,1);   // 3
    console.log(output1);
    console.log(output2)

提交回复
热议问题