Hey! Can you create me a JS function that sums an array of integers?

Asked on 07/31/2024

1 search

Sure! Here is a simple JavaScript function that sums an array of integers:

function sumArray(arr) {
    return arr.reduce((acc, curr) => acc + curr, 0);
}

// Example usage:
const numbers = [1, 2, 3, 4, 5];
console.log(sumArray(numbers)); // Output: 15

This function uses the reduce method to iterate over the array and accumulate the sum of its elements. The initial value of the accumulator (acc) is set to 0.

If you have any questions about the content presented by Apple at WWDC, feel free to ask! For example, you might be interested in sessions like "Explore Swift performance" or "Support real-time ML inference on the CPU".