gpt-3.5-turbo-0125· OpenAI
WildChat-4.8M (AI2)
how to find factorial in javascript
- User
how to find factorial in javascript
- Assistant
You can find the factorial of a number in JavaScript by writing a recursive function that calculates the factorial of a given number. Here is an example of how you can do this:
function factorial(n) { if (n === 0) { return 1; } return n * factorial(n - 1); } // Example usage console.log(factorial(5)); // Output: 120In the above code, the
factorialfunction takes a numbernas an argument and recursively calculates the factorial of that number. If the number is 0, it returns 1, as the factorial of 0 is 1. Otherwise, it multiplies the number by the factorial of the number minus one, until it reaches 0.