I'm trying to write a JavaScript function that lets me do the following: if I call
const multiply = define("x", "y", "x * y");
I want multiply to be
function(x) {
return function(y) {
return x * y;
}
}
The number of arguments is unknown beforehand. The last one is always the final return, every other argument is each a paramenter of an inner function. What should my define function be?
I know I can just write const multiply = x => y => x * y, but I need the code to be as user-friendly as possible and higher-order functions like this are not completely clear to someone who doesn't use them often.
I've tried using the Function constructor but the most I've managed to come up with returns me function(x, y) { return x * y; } which is not what I'm looking for.
My idea is building the function step-by-step, so first I have to create a function f that takes y and returns x * y, then I have to create g that takes x and returns f(y). But this is where I'm stuck.
Can anyone give me a clue on how to solve this problem? Thanks.