I have to write a single function that must be invoked by either
sum(2,3); //5
//or
sum(2)(3); //5
I write this piece of code
function sum (a,b){
return a + b;
}
sum(2,3);
And I get the 'TypeError: number is not a function'. Why?
I have to write a single function that must be invoked by either
sum(2,3); //5
//or
sum(2)(3); //5
I write this piece of code
function sum (a,b){
return a + b;
}
sum(2,3);
And I get the 'TypeError: number is not a function'. Why?
You can do something like:
function sum(a,b) {
return arguments.length>1? a+b : function (b) { return a + b };
}
You should use curried functions:
function sum(a, b) {
if (b === undefined) {
return function (b) {
return a + b;
}
}
return a + b;
}
// sum(1, 2) === sum(1)(2)
You can only call the function in this manner.
sum(2,3);