[Solved] JavaScript Multiple ways to call a function [closed]


Below is the code that I came up with to solve this naively. Please feel free to ask questions as to why it works or what could be done to improve it.

var calculate = function () {
    var arg1 = arguments[0];
    if (arguments.length === 2) {
        return arg1 + arguments[1];
    }
    if (arguments.length === 1) {
        return function (secondNum) {
            return secondNum + arg1;
        }
    }

}

As you can see, there are a few concepts at work in this function. The arguments array is available inside any JavaScript function. It allows you to examine all the arguments that were passed into the function. As you can see, I first check if we have 2 arguments, that clues me in to the fact that I should add them together.

In the second case, I only got one argument, so I’ll return a function that will help me accomplish the function(x)(y) case.

10

solved JavaScript Multiple ways to call a function [closed]