- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Consecutive elements sum array in JavaScript
We are required to write a JavaScript function that takes in an array of Numbers and returns a new array with elements as the sum of two consecutive elements from the original array.
For example, if the input array is −
const arr1 = [1, 1, 2, 7, 4, 5, 6, 7, 8, 9];
Then the output should be −
const output = [2, 9, 9, 13, 17]
Example
The code for this will be −
const arr11 = [1, 1, 2, 7, 4, 5, 6, 7, 8, 9]; const consecutiveSum = arr => { const res = []; for(let i = 0; i < arr.length; i += 2){ res.push(arr[i] + (arr[i+1] || 0)); }; return res; }; console.log(conseutiveSum(arr1));
Output
The output in the console −
[ 2, 9, 9, 13, 17 ]
- Related Articles
- Maximum sum of n consecutive elements of array in JavaScript
- Return the sum of two consecutive elements from the original array in JavaScript
- Compress array to group consecutive elements JavaScript
- Absolute sum of array elements - JavaScript
- Check if three consecutive elements in an array is identical in JavaScript
- Sum all similar elements in one array - JavaScript
- Sum identical elements within one array in JavaScript
- Sum of consecutive numbers in JavaScript
- Thrice sum of elements of array - JavaScript
- Rearrange an array to minimize sum of product of consecutive pair elements in C++
- Find consecutive elements average JavaScript
- JavaScript - Constructs a new array whose elements are the difference between consecutive elements of the input array
- Sum of distinct elements of an array in JavaScript
- Sum of distinct elements of an array - JavaScript
- Finding desired sum of elements in an array in JavaScript

Advertisements