Sum of Odd Integers in Array

This problem asks us to return the sum of all odd integers in an array. For example, given the array [1, 2, 3, 4, 5], the output would be 9 .

Problem

Given an array of integers, return the sum of the odd integers.
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 the current 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 only adds the odd numbers to the sum.

A.I. Evaluation of the Solution

This is a great solution! It is very efficient and only iterates through the array once. It also only adds the odd numbers to the sum, which is exactly what the question asks.

Evaluated at: 2022-11-25 16:15:27