• Register
Welcome to Kodlogs, programming questions and answer website.
0 votes
8 views

Problem :

I have the very unique question from the programming background. They want me to write the function which should multiply (a) and (b) but not writing it like below one:

multiplyfunc(a,b){

return a*b;

}

They want me to do a math with below approach

multiplyfunc(a)(b)

Is it possible to do it?

by (7.5k points)  

1 Answer

0 votes

Solution :

I had also faced the similar issue in the recent past. I did lot of research on it and found the solution on it. This is the problem with the people getting started..

You should make the function that returns another function as shown below.

const multiplfunc = a => b => a * b;
console.log(multiplyfunc(4)(3));

 

OR

Try with the function that returns the function as shown below :

function multiplyfunc (a) {
  return function (b) {
    return a * b;
  };
};

console.log(multiplyfunc(4)(3));

 

OR

Currying is the process used to reduce the functions of more than one argument to the functions of one argument with the help of the lambda calculus.

f(n, m) --> f'(n)(m)

Example:

multiplyfunc = (n, m) => (n * m)

multiplyfunc(3, 4) === 12 // true

curryedMultiplyfunc = (n) => ( (m) => multiplyfunc(n, m) )

triplefunc = curryedMultiplyfunc(3)

triplefunc(4) === 12 // true

 

I hope it helps you in solving your issue.

by (34.3k points)  
...