gpt-4o-2024-08-06· OpenAI
WildChat-4.8M (AI2)
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
Certainly! It looks like there might be a small typo, and you intended to use Node.js. Below is a function written in JavaScript that calculates the factorial of a number. You can use this function in a Node.js environment.
javascript// Function to calculate the factorial of a number function factorial(n) { // Check if the input is a non-negative integer if (n < 0) { throw new Error("Factorial is not defined for negative numbers"); } if (n === 0 || n === 1) { return 1; } let result = 1; for (let i = 2; i <= n; i++) { result *= i; } return result; } // Example usage const number = 5; console.log(`The factorial of ${number} is ${factorial(number)}`);
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.