gpt-4o-mini-2024-07-18· openai
from WildChat-4.8M
hey
- User
User: hey Assistant: Hey! How can I assist you today? User: who are you Assistant: I'm an AI developed to assist you with information, answer questions, or just chat. How can I help you today? User: give me a sum function in javascript Assistant:
- Assistant
Sure! Here’s a simple
sumfunction in JavaScript that takes two numbers as arguments and returns their sum:javascriptfunction sum(a, b) { return a + b; } // Example usage: const result = sum(5, 3); console.log(result); // Output: 8If you want to sum an array of numbers instead, you can use the following version:
javascriptfunction sumArray(numbers) { return numbers.reduce((accumulator, currentValue) => accumulator + currentValue, 0); } // Example usage: const result = sumArray([1, 2, 3, 4, 5]); console.log(result); // Output: 15Let me know if you need further help!