I have an oddball of a question from a code quiz. They expect me to write a function that multiplies (a) and (b) but instead of writing it like:
multiply(a,b) {
return a*b;
}
They expect me to do the math with:
multiply(a)(b)
Is it possible ?
I have an oddball of a question from a code quiz. They expect me to write a function that multiplies (a) and (b) but instead of writing it like:
multiply(a,b) {
return a*b;
}
They expect me to do the math with:
multiply(a)(b)
Is it possible ?
Share Improve this question edited Jun 11, 2022 at 14:12 Nikola Lukic 4,2586 gold badges48 silver badges80 bronze badges asked Mar 26, 2018 at 0:08 OjoOjo 451 gold badge1 silver badge8 bronze badges 1- 3 this is called currying – A. L Commented Mar 26, 2018 at 0:09
3 Answers
Reset to default 4Make a function that returns another function.
const multiply = a => b => a * b;
console.log(multiply(4)(3));
Looks like they'd like you to use currying. Currying allows you to reduce functions of more than one argument to functions of one argument. The link below has a very similar example to what your employer is looking for.
Helpful Resource: https://blog.benestudio.co/currying-in-javascript-es6-540d2ad09400
I hope this answers your question.
It's a function that returns a function
function multiply (a) {
return function (b) {
return a * b;
};
};
console.log(multiply(4)(3));