Sum of odd integers in an array

The function should take an array of integers as input and return the sum of the odd integers in the array.

Problem

Given an array of integers, write a function that returns the sum of the odd integers in the array.
Example input:
[1, 2, 3, 4, 5]
Example output:
9

Solution

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

function sumOdd(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 odd. 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 is a great solution! The candidate has thought through the problem and provided a detailed explanation of their approach. This solution is optimal because it uses a for loop to iterate through the array and check if each element is odd. 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.

Evaluated at: 2022-11-27 02:16:26