Skip to content Skip to sidebar Skip to footer

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;

Solution 1:

Here you go. Should be self-evident:

var math = 
{
    'factorial': functionfactorial(n) 
    {
        if (n <= 1)
          return1;
        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);

Post a Comment for "How To Call A Javascript Function Expression"