Write a function in Nodx.js that calculates the factorial of a number.
- User
Write a function in Nodx.js that calculates the factorial of a number.
- Assistant
It looks like there's a small typo in your request. I assume you meant to refer to Node.js, so I will provide a function using JavaScript that can run in a Node.js environment.
Here's a simple recursive function to calculate the factorial of a number:
javascriptfunction factorial(n) { if (n < 0) { throw new Error('Factorial is not defined for negative numbers'); } if (n === 0 || n === 1) { return 1; } return n * factorial(n - 1); } // Example usage: console.log(factorial(5)); // Output: 120 console.log(factorial(0)); // Output: 1
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.