# Return the sum of two consecutive elements from the original array in JavaScript

JavascriptWeb DevelopmentFront End Technology

#### JavaScript for beginners

Best Seller

74 Lectures 10 hours

#### Modern Javascript for Beginners + Javascript Projects

Most Popular

112 Lectures 15 hours

#### The Complete Full-Stack JavaScript Course!

Best Seller

96 Lectures 24 hours

An Array is known as collection of similar data elements which are stored at contiguous memory locations. Array is such a simple data structure where we can access each data element in the array with the help index numbers, which starts from 0.

The following is the basic declaration of array in JavaScript −

const mobiles = ["SAMSUNG", "ONEPLUS", "APPLE"];

## Sum of two consecutive elements of an array

We are required to write a JavaScript function that takes in an array of Numbers and returns a new array with elements as sum of two consecutive elements from the original array.

### Input output scenarios

Consider there is an array having some elements in it and should get the sum of consecutive elements, for example consider the array arr = [1,2,3,4].

Add arr[0] and arr[1] and then arr[1] and arr[2], next arr[2] and arr[3].

Input = [1, 2, 3, 4, 5]
Output = 3, 5, 7, 9

Consider another scenario with the same array as in the above example arr = [1,2,3,4], Now add arr[0] and arr[1], and arr[2] and arr[3]

Input = [1, 2, 3, 4]
Output = 3, 7

### Algorithm

Following is the step by step process to calculate the sum of consecutive elements of an array. Assume an array arr=[1, 2, 3, 4, 5].

Step 1 − First add arr[0] and arr[1], the sum will be 3.

Step 2 − Now, add arr[1] and arr[2], the sum is 5.

Step 3 − Add arr[2] and arr[3], after adding sum will be 7.

Step 4 − Finally add arr[3] and arr[4], the sum is 9.

The sum of two consecutive elements from the array will be: 3, 5, 7, 9. Now let’s implement this using JavaScript.

### Example 1

Sum of consecutive elements

In the example below, we have created and array with some elements present in it. Also created a result array (res_arr) to push the values of consecutive numbers after getting added from original array.

<!DOCTYPE html> <html> <title>Sum of two consecutive elements</title> <head> <button onClick = "soce()">Click!</button> <p id = "para"> </p> <script> function soce(){ const array = [7, 18, 4, 16, 2, 5]; let n = array.length; function con_sum(array) { const resArr = []; for(let x = 0; x + 1 < n; x ++){ resArr.push(array[x] + (array[x+1])); }; return resArr; }; document.getElementById("para").innerHTML = "sum of consecutive elements in the given array are: " + con_sum(array); }; </script> </head> </html>

### Example 2

Sorting the array and then sum of consecutive elements

In the example below, we have an array with elements in it. First we sorted the given array and then performed the sum of two consecutive elements. The values of sum of two consecutive elements will be pushed into result array (res_arr) from the original array.

<!DOCTYPE html> <html> <head> <title>Sum of two consecutive elements</title> <script> const array = [5, 7, 2, 6, 1]; document.write("The array before sorted: ", array, "<br>"); array.sort(function( a , b){ if(a > b){ return 1; } if(a < b){ return -1; } return 0; }); document.write("The array after sorted: ", array, "<br>"); function con_sum(array){ let n = array.length; const res_arr = []; for(let x = 0; x + 1 < n; x ++){ res_arr.push(array[x] + (array[x+1])); }; return res_arr; }; document.write("sum: "); document.write(con_sum(array)); </script> </head> </html>

### Another Solution

Let’s see another perspective of adding two consecutive elements in array. Assume an array arr=[1, 2, 3, 4, 5, 6].

• Add the elements at index arr[0] and arr[1], the sum will be 3.

• Now add the elements at indexes arr[2] and arr[3], the sum is 7.

• Finally add arr[4] and arr[5], the sum will be 11.

• The sum of consecutive numbers from the array will be: 3, 7, 11.

### Example 3

In this example below, we have an array with elements. Here we have performed the sum of consecutive elements as shown in the above implementation. The values of sum of two consecutive elements will be pushed into result array (res_arr) from the original array.

<!DOCTYPE html> <html> <head> <title>Sum of two consecutive elements</title> <button onClick = "soce()"> Click </button> <p id = "para"> </p> <script> function soce(){ const array = [10, 5, 6, 6, 3, 9, 8, 5]; let n = array.length; function con_sum(array){ const resArr = []; for(let i = 0; i < n; i += 2){ resArr.push(array[i] + (array[i+1] || 0)); }; return resArr; }; document.getElementById("para").innerHTML = con_sum(array); }; </script> </head> </html>
Updated on 23-Sep-2022 11:09:39