0

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);
    }
};
2
  • 3
    math.factorial(n). Commented Jan 19, 2015 at 2:13
  • Indentation is so 2014 Commented Jan 19, 2015 at 2:14

1 Answer 1

1

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);

Sign up to request clarification or add additional context in comments.

2 Comments

It may be worth noting that the quotes around factorial are not necessary :)
That's the same code from the OP's question. The comment would be better there...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.