Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Return the sum of two consecutive elements from the original array in JavaScript
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].
The sum of two consecutive elements from the array will be: 3, 5, 7, 9. Now let's implement this using JavaScript.
Method 1: Adjacent Elements Sum
In this approach, we sum adjacent elements: arr[0]+arr[1], arr[1]+arr[2], arr[2]+arr[3], etc.
<!DOCTYPE html>
<html>
<head>
<title>Sum of two consecutive elements</title>
</head>
<body>
<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>
</body>
</html>
sum of consecutive elements in the given array are: 25,22,20,18,7
Method 2: Sorting Array First
In this approach, we first sort the array and then perform the sum of adjacent consecutive elements.
<!DOCTYPE html>
<html>
<head>
<title>Sum of two consecutive elements</title>
</head>
<body>
<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>
</body>
</html>
The array before sorted: 5,7,2,6,1 The array after sorted: 1,2,5,6,7 sum: 3,7,11,13
Method 3: Pairing Elements (Skip One)
Let's see another approach of adding consecutive elements in pairs. For array [1, 2, 3, 4, 5, 6], we add arr[0]+arr[1], arr[2]+arr[3], arr[4]+arr[5].
<!DOCTYPE html>
<html>
<head>
<title>Sum of two consecutive elements</title>
</head>
<body>
<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>
</body>
</html>
15,12,12,13
Comparison
| Method | Approach | Array [1,2,3,4] | Output Length |
|---|---|---|---|
| Adjacent Sum | arr[i] + arr[i+1] | [3, 5, 7] | n-1 |
| Pairing | arr[i] + arr[i+1], skip by 2 | [3, 7] | n/2 |
Conclusion
JavaScript provides flexible approaches for summing consecutive array elements. Choose adjacent summing for overlapping pairs or pairing method for non-overlapping pairs based on your requirements.
