# [面试题目]如何实现multi(2)(3)(4)=24?

------------------分割线---------------------

``````function multi(n){
var fn = function(x) {
return multi(n * x);
};

fn.toString = function() {
return n;
};

return fn;
}
``````

------------------分割线---------------------

14 个回答
• 519

``````function multi(n){
var fn = function(x) {
return multi(n * x);
};

fn.valueOf = function() {
return n;
};

return fn;
}

multi(1)(2)(3)(4) == 24; // true``````

``````var baseFun = function(a, b, c){
return a * b * c;
}
var multi = _.curry(baseFun);
console.log(multi(2)(3)(4));``````

``````function multi(v, last, times){
times = (times || 0) + 1;
last = (last || 1) * v;
if(times < 3) {
return function(v) {
return multi(v, last, times);
}
} else {
return last;
}
}
console.log(multi(2)(3)(4));``````
``````function curry(fn){
var value;
var callback = function(next){
value = typeof value === "undefined" ? next : fn.apply(null,[value,next]);
return callback;
}
callback.valueOf = callback.toString = function(){
return value;

}
return callback
}
//加
return x + y
}
//减
function minus(x,y){
return x -y
}
//乘
function multiply(x,y){
return x * y;
}
//除
function divide(x,y){
return x / y;
}
curry(minus)(2)(3)(4)(5)(6) //2-3-4-5-6=-16
curry(multiply)(2)(3)(4)(5)(6) //2*3*4*5*6=720
curry(divide)(2)(3)(4)(5)(6) //2 / 3 / 4/ 5 /6 = 0.00555555...``````
• 26.5k

``````var multi = function(num1){
return function(num2){
return function(num3){
return num1 * num2 * num3;
};
};
};``````

var baseFun = function(a, b, c){
return a b c;
}
var multi = _.curry(baseFun);
console.log(multi(2)(3)(4));

``````var multi = x => y => z => x * y * z;
``````
• 5.6k
``````function multi(num){
return function(secondNum){return function(thirdNum){return num*secondNum*thirdNum}}
}``````

function curry (fn) {
var slice = Array.prototype.slice,
old_args = slice.call(arguments, 1);
return function () {
var new_args = slice.call(arguments),
args = old_args.concat(new_args);
return fn.apply(null, args);
}
}

var add = function (x, y, z) {
return x+y+z;
}

//一步curry化

//两步curry化

• 18.4k

``````var multi=function(arg0){
return function(arg1){
return function(arg2){
return arg0*arg1*arg2
}
}
};``````

``````var multi=(function(){

var result=1;
return function __multi__(arg){
if(!arg){
return result;
}
result=result*arg;
return __multi__;
};
}());
···
console.log(multi(2)(3)(5)());``````
``````var add = function (m) {
function temp (n) {
}

temp.toString = function () {
return m;
}

return temp;
}``````

http://roshanca.com/2012/java...

• 1
• 新人请关照

currying。撸主可以去查一下~

``````var curry = function(func) {
var that=this;
var curry_func = function() {
var this_func = arguments.callee;
var new_params = this_func.params.slice();
for(var i=0;i<arguments.length;i++)
new_params.push(arguments[i]);
if(new_params.length >= func.length)
return func.apply(that, new_params);
else{
// dirty way to copy function
eval('var new_func =' + this_func.toString());
new_func.params = new_params;
return new_func;
}
};
curry_func.params = [];
return curry_func;
};

// 原函数
var test_func = function(a, b, c) { return a*b*c; };

var f0 = curry(test_func);

// 实现科里化功能
console.log(f0(2,3,4));
console.log(f0(2)(3)(4));
console.log(f0(2,3)(4));

// 科里化结果不会互相影响
f1 = f0(2,3);
console.log(f1(10));
console.log(f1(11));``````