To find the sum of all even numbers in an array, iterate through the array and add the even numbers to a running total.
- Initialize a variable to store the sum of even numbers.
- Iterate through the array and check if each number is even (i.e., divisible by 2).
- If the number is even, add it to the sum.
- Return the final sum.
function sumEvenNumbers(arr) {
let sum = 0;
for (const num of arr) {
if (num % 2 === 0) {
sum += num;
}
}
return sum;
}
// Example usage
console.log(sumEvenNumbers([1, 2, 3, 4, 5, 6])); // Output: 12
console.log(sumEvenNumbers([10, 15, 20, 25])); // Output: 30
This method has a time complexity of O(n), where n is the length of the array.
Tags: basic, JavaScript, Arrays, Algorithm