How can I change this function so that it can be run with one set of parameters or two, and get the same result?
You can almost do it, but I'm trying my best to think of a good reason.
Here's how: you discover how many arguments your function received, and if it received only one, you return the function instead of the number - and add this function to the second number if it is called:
function add(a,b) { if (arguments.length === 1) { return function(b2) {
I said โalmostโ above, because only because the add function received only one argument, which does not guarantee that the caller will call the result. They could write:
var x = add(10);
... and never call the function that x now refers to.
source share