要求写一个方法 f,使得 +f(2, 3) // 5 +f(2)(3) // 6 +f(2, 3)(4) // 20 +f(2, 3)(4)(5) // 100 +f(1)(1)(1)(1, 1, 1) // 3 括号里为加,括号与括号间为乘
let array = [];
let fun = function(...arg) {
console.log(arg)
if(arg != null){
let sum = arg.reduce((tmp, item, index)=>{
// console.log(tmp, item, index);
console.log(arg);
console.log(tmp + item);
array.push(tmp + item);
})
return fun;
}else{
resultSum(array);
}
};
let resultSum = function(arr){
let num = 1;
arr.forEach(item=>{
num *= item;
})
console.log(num);
return num;
}
//sum([1,2,3,4])
fun(1, 1, 1)(2, 3);