Finding Sum of Alternate Elements of the Array using JavaScript Last Updated : 25 Jun, 2024 Comments Improve Suggest changes Like Article Like Report Given an array, our task is to find the sum of alternative elements of the array using JavaScript. Example: Input: array = { 1, 2, 3, 4, 5, 6, 7, 8}Output: 16 Explanation:Sum of alternate elements of array = 1 + 3 + 5 + 7 = 16 Table of Content Using a for-loopUsing reduce Method Using Filter and Reduce MethodsUsing a for-loopIn this approach, The function sumOAlt initializes a sum variable and iterates through the array, incrementing by 2 to access alternate elements. In each iteration, it adds the current element to the sum. Finally, the function returns the total sum of the alternate elements. Example: The example below shows how to Find the sum of alternative elements of the array Using a for-loop. JavaScript function sumOAlt(arr) { // Initialize sum let sum = 0; // Iterate over array for (let i = 0; i < arr.length; i += 2) { // Add to sum sum += arr[i]; } return sum; } const array1 = [1, 2, 3, 4, 5, 6, 7, 8]; console.log("Sum of alternate elements:", sumOAlt(array1)); OutputSum of alternate elements: 16 Time complexity: O(n). Space complexity: O(1). Using reduce Method The function sumOAlt utilizes the reduce() method to iterate through the array. It checks if the current index is even, and then adds the element to the accumulator sum. Finally, it returns the sum of alternate elements in the array using reduce. Example: The example below shows how to find the sum of alternative elements of the array Using the reduce method. JavaScript function sumOAlt(arr) { // Use the reduce method return arr.reduce((sum, element, index) => { if (index % 2 === 0) { return sum + element; } return sum; }, 0); } const array1 = [1, 2, 3, 4, 5, 6, 7, 8]; console.log("Sum of alternate elements:", sumOAlt(array1)); OutputSum of alternate elements: 16 Time complexity: O(n) Space complexity: O(1) Using Filter and Reduce MethodsIn this approach, we first filter out the alternate elements of the array by using the filter method and then calculate their sum using the reduce method. This approach is both concise and efficient, leveraging JavaScript's array methods for a clean solution. Example: JavaScript function sumOfAlternateElements(arr) { return arr .filter((_, index) => index % 2 === 0) .reduce((sum, num) => sum + num, 0); } // Examples const array1 = [1, 2, 3, 4, 5, 6, 7, 8]; console.log(sumOfAlternateElements(array1)); // Output: 16 const array2 = [10, 20, 30, 40, 50]; console.log(sumOfAlternateElements(array2)); // Output: 90 const array3 = [5, 15, 25, 35, 45, 55]; console.log(sumOfAlternateElements(array3)); // Output: 75 Output16 90 75 Comment More infoAdvertise with us Next Article Finding Sum of Alternate Elements of the Array using JavaScript bug8wdqo Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Program Similar Reads Sum of Distinct Elements of an Array using JavaScript One can find a Sum of distinct elements (unique or different numbers) present in an array using JavaScript. Below is an example to understand the problem clearly. Example:Input: [ 1,2, 3, 1, 3, 4, 5, 5, 2] Output: 15 Explanation: The distinct elements present in array are: 1, 2, 3, 4 and 5 Sum = 1 + 4 min read Alternative Sorting of an Array using JavaScript Alternative sorting means we have to arrange the elements of an array in such a way that the first element is the first maximum and the second element is the first minimum, the third element is the second maximum, the fourth element is the second minimum, and so on.Example:Input: array = [5, 6, 2, 4 3 min read JavaScript Program to Find the First Non-Repeated Element in an Array Finding the first non-repeated element in an array refers to identifying the initial occurrence of an element that does not occur again elsewhere within the array, indicating uniqueness among the elements.Examples: Input: {-1, 2, -1, 3, 0}Output: 2Explanation: The first number that does not repeat i 5 min read JavaScript Program to Search an Element in an Array A JavaScript program searches an element in an array by iterating through each item and comparing it with the target value. If found, it returns the index; otherwise, it returns -1, indicating absence. Following are the ways to search for an element in an array: Table of Content Using Recursive Appr 4 min read JavaScript - Access Elements in JS Array These are the following ways to Access Elements in an Array:1. Using Square Bracket NotationWe can access elements in an array by using their index, where the index starts from 0 for the first element. We can access using the bracket notation.JavaScriptconst a = [10, 20, 30, 40, 50]; const v = a[3]; 2 min read JavaScript Program for the Minimum Index of a Repeating Element in an Array Given an array of integers arr[], The task is to find the index of the first repeating element in it i.e. the element that occurs more than once and whose index of the first occurrence is the smallest. In this article, we will find the minimum index of a repeating element in an array in JavaScript.E 5 min read Remove Least Elements to Convert Array to Increasing Sequence using JavaScript Given an array of numbers, our task is to find the minimum number of elements that need to be removed from the array so that the remaining elements form an increasing sequence. Examples: Input: arr = [5, 100, 6, 7, 100, 9, 8];Output: No of elements removed =3, Array after removing elements =[ 5, 6 , 3 min read JavaScript- Delete all Occurrences in a JS Array These are the following ways to remove all Occurrences of an element in a JS Array: 1. Using filter() method( Simple and Easy for Long Arrays)The filter() method creates a new array with all elements that passed the test implemented by the given callback function.JavaScriptfunction fun(a, e) { if (! 2 min read JavaScript Program to Rearrange Array Alternately Rearranging an array alternately means, If we have a sorted array then we have to rearrange the array such that the first largest element should come at index-0(0-based indexing), first smallest element should come at index-1, similarly, second largest element should come at index-3 and second small 7 min read JavaScript Program to Count Unequal Element Pairs from the Given Array Unequal element pairs in an array refer to pairs of distinct elements within the array that have different values. These pairs consist of two elements that are not equal to each other, highlighting their inequality when compared in the context of the array's values.Examples:Input: arr[] = {6, 5, 2, 4 min read Like