Function multiply(a, b){ a * b }

0 votes
115 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?

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.

36.1k points

Related questions

1 vote
2 answers 554 views
554 views
Problem: I recently started learning python programming. This is my very first attempt to write a code run the program. I tried to compile my program but it throwing me back an error. def multiply(a, b): a * b What should I do now?
0 votes
2 answers 58 views
58 views
Problem: I am new to programming, so need query to get query to return max number from 3 integer? Write the definition of a method max that has three int parameters and returns the largest? Can anyone guide with correct code?
1 vote
1 answer 39 views
39 views
Problem : I am very new to Vanilla JavaScript. Currently I am learning Vanilla JavaScript as I need to use the Vanilla JavaScript for my current project. I have already written many functions one of them has a button in it which should open the menu and it only ... spent a lot of time in fixing target id issue so looking for Vanilla JavaScript experts help who can suggest quick fix on above error.
0 votes
1 answer 11 views
11 views
Problem: Hello guys, I was learning the function in python, and wondering can we use function within a function then I google some stuff then got to know that function within the function is better way of implementing the algorithm we are implementing, can you guys please show it with an example.
0 votes
1 answer 252 views
252 views
Problem: I am new, I need help, can anyone help me by filling in the blanks? The ________ built-in function is used to read a number that has been typed on the keyboard? A. get() B. input() C. keyboard() D. read()