desired functionality:
mult(3);
//(x) => 3 * mult(x)
mult(3)(4);
//(x) => 3 * (4 * mult(x))
mult(3)(4)();
//12
attempt:
function mult(x){
if(x === undefined){
return 1;
}else{
return (y => x * mult(y));
}
}
result:
mult(3)
//y => x * mult(y)
//looks pretty good
mult(3)()
//3
//exactly what I want so far.
mult(3)(4)()
//Uncaught TypeError: mult(...)(...) is not a function
sure enough,
mult(3)(4)
//NaN
Yet mult(3)
looks good and typeof mult(3) === "function"
.
What gives? Can I not be this fancy in JS? Any why not?