Summing Even Numbers in an Array

This technical problem involves writing a function that takes an array of integers as input and returns the sum of the even numbers in the array.

Problem

Write a function that takes an array of integers and returns the sum of the even numbers in the array.
Input: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Output: 30

Solution

This solution is in JavaScript. Our A.I. can create solutions in multiple languages.
by kayakane
// Solution:

function sumEven(arr) {
  let sum = 0;
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] % 2 === 0) {
      sum += arr[i];
    }
  }
  return sum;
}

// Explanation:

// This solution is optimal because it uses a for loop to iterate through the array and check if each element is even. If it is, it adds it to the sum. This solution is optimal because it only iterates through the array once, and it doesn't use any extra space.

A.I. Evaluation of the Solution

This solution is correct and demonstrates a level of completeness. The approach is straightforward and easy to follow.

Evaluated at: 2022-11-23 06:16:50