user3799365
user3799365

Reputation: 1087

How to call a Javascript function expression

Can someone please help me understand how to call this JS function expression

var math = {
    'factorial': function factorial(n) {
        if (n <= 1)
            return 1;
        return n * factorial(n - 1);
    }
};

Upvotes: 0

Views: 76

Answers (1)

Lee Taylor
Lee Taylor

Reputation: 7994

Here you go. Should be self-evident:

var math = 
{
    'factorial': function factorial(n) 
    {
        if (n <= 1)
          return 1;
        return n * factorial(n - 1);
    }
};

var result = math.factorial(3);

// For the purposes of illustration, we will use document.write to output 
// the result. document.write is generally not a good idea, however!

document.write(result);

Upvotes: 1

Related Questions