JavaScript Program to Find Sum of Even Numbers of an Array
Last Updated :
26 Feb, 2024
In JavaScript, working with arrays is a basic operation. We have to sum all the even numbers present in the array. We can check the number if it is even or not by the use of the % operator.
These are the following ways to find the sum of Even numbers in an Array:
Sum of Even Numbers of an Array using Iterative Approach
This method iterates through each element in the array and checks if it's even. If it is, the element is added to a running total variable, which stores the cumulative sum of all even numbers encountered so far.
Example: The function `sumOfEvenNumbers` calculates the sum of even numbers in an array using a loop and conditional statements.
JavaScript
function sumOfEvenNumbers(arr) {
let sum = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] % 2 === 0) {
sum += arr[i];
}
}
return sum;
}
const numbers = [1, 2, 3, 4, 5];
const evenSum = sumOfEvenNumbers(numbers);
console.log("Sum of even numbers:", evenSum);
OutputSum of even numbers: 6
Sum of Even Numbers of an Array using filter() and reduce() methods
This method provides a simpler answer by using built-in array functions. By using the filter method, an array is created that is limited to the even elements present in the original array. The filtered array is then iterated by using the reduce method, which adds each element to the sum.
Example: The `sumOfEvenNumbers` function filters the even numbers from the input array using the `filter` method and then calculates their sum using the `reduce` method.
JavaScript
function sumOfEvenNumbers(arr) {
return arr.filter(num => num % 2 === 0)
.reduce((acc, num) => acc + num, 0);
}
const numbers = [1, 2, 3, 4, 5];
const evenSum = sumOfEvenNumbers(numbers);
console.log("Sum of even numbers:", evenSum);
OutputSum of even numbers: 6
Sum of Even Numbers of an Array using Recursive Approach
This method solves the problem by using recursion. It defines a function that accepts three parameters: the array, the current index, and the sum of the total. After checking that the array has ended in the base case, the function calls itself recursively with the next element and, if the current number is even, changes the total.
Example: Recursive function `sumOfEvenNumbers` computes the sum of even numbers in an array. It recursively processes the array, adding even numbers to the sum. The base case returns 0 if the array is empty.
JavaScript
function sumOfEvenNumbers(arr) {
// Base case: If the array is empty, return 0.
if (arr.length === 0) {
return 0;
}
const firstElement = arr[0];
const restOfArray = arr.slice(1);
if (firstElement % 2 === 0) {
return firstElement +
sumOfEvenNumbers(restOfArray);
} else {
return sumOfEvenNumbers(restOfArray);
}
}
const numbers = [1, 2, 3, 4, 5, 6];
const evenSum = sumOfEvenNumbers(numbers);
console.log("Sum of even numbers:", evenSum);
OutputSum of even numbers: 12
Sum of Even Numbers of an Array using Using forEach Loop
This method uses forEach to iterate through the array, providing an alternative to the normal loop. It keeps readability and efficiency while providing a slightly more simple solution.
Example: The `sumOfEvenNumbers` function calculates the sum of even numbers in an array using a forEach loop. It iterates through the array, adding each even number to the sum. The example demonstrates its usage by finding the sum of even numbers in an array and printing the result.
JavaScript
function sumOfEvenNumbers(arr) {
let sum = 0;
arr.forEach(num => {
// Check if the current element is even.
if (num % 2 === 0) {
// If the current element
// is even, add it to the sum.
sum += num;
}
});
// Return the total sum of even numbers.
return sum;
}
const numbers = [1, 2, 3, 4, 5, 6];
const evenSum = sumOfEvenNumbers(numbers);
console.log("Sum of even numbers:", evenSum);
OutputSum of even numbers: 12
Similar Reads
JavaScript Program to Find Sum of Odd Numbers in an Array In JavaScript, working with arrays is a common task. Often one needs to manipulate the elements within an array, such as finding specific values or calculating their sum. Below are the approaches to find the sum of all odd numbers within an array: Table of Content 1. Using a loop2. Using the filter
4 min read
JavaScript Program to Count Even and Odd Numbers in an Array In this article, we will write a program to count Even and Odd numbers in an array in JavaScript. Even numbers are those numbers that can be written in the form of 2n, while Odd numbers are those numbers that can be written in the form of 2n+1 form. For finding out the Even and Odd numbers in an arr
4 min read
JavaScript program to print even numbers in an array Given an array of numbers and the task is to write a JavaScript program to print all even numbers in that array. We will use the following methods to find even numbers in an array: Table of Content Method 1: Using for Loop Method 2: Using while Loop Method 3: Using forEach LoopMethod 4: Using filter
5 min read
Sum of Squares of Even Numbers in an Array using JavaScript JavaScript program allows us to compute the sum of squares of even numbers within a given array. The task involves iterating through the array, identifying even numbers, squaring them, and summing up the squares. There are several approaches to find the sum of the square of even numbers in an array
2 min read
Java Program to Find the Sum of First N Odd & Even Numbers When any number which ends with 0,2,4,6,8 is divided by 2 that is an even number. And when any number ends with 1,3,5,7,9 is not divided by two is an odd number. Example: Input : 8 Output: Sum of First 8 Even numbers = 72 Sum of First 8 Odd numbers = 64Approach #1: Iterative Create two variables eve
3 min read
Java Program to Print the Elements of an Array Present on Even Position The task is to print all the elements that are present in even position. Consider an example, we have an array of length 6, and we need to display all the elements that are present in 2,4 and 6 positions i.e; at indices 1, 3, 5. Example: Input: [1,2,3,4,5,6] Output: 2 4 6 Input: [1,2] Output: 2 Appr
2 min read
Java Program to Print the Elements of an Array Present on Odd Position An array stores the collection of data of the same type. It is a fixed-size sequential collection of elements of the same type. In an array, if there are "N" elements, the array iteration starts from 0 and ends with "N-1". The odd positions in the array are those with even indexing and vice versa. E
3 min read
C Program to Find the Sum of Fibonacci Numbers at Even Indexes up to N Terms Here, we will build a C Program to Find the Sum of Fibonacci numbers at even indexes up to N termsFibonacci Numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, â¦â¦Input: N = 5Output:88 // Sum of the elements at even indexes = 0 + 1 + 3 + 8 + 21 + 55 = 88Approach:This approach includes solving the
2 min read
PHP | Separate odd and even elements from array without using loop You are given an array of n elements in PHP. You have to separate the elements from the array based on the elements are odd or even. That is, print odd array and even array separately without traversing the original array or using any loop. Examples: Input : array(2, 5, 6, 3, 0) Output : Odd array:
3 min read
Sum Even and Odd Values with One For-Loop and No If-Condition Using Python Summing even and odd numbers in a list is a common programming task. Typically, we'd use an if condition to check whether a number is even or odd. However, we can achieve this in Python efficiently using arithmetic operators and list slicing, without relying on if conditions. In this article, we'll
4 min read