
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Add two consecutive elements from the original array and display the result in a new array with 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 arrayOne = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
Then the output should be −
const newArrayOne = [1, 5, 9, 13, 17]
Let's write the code for this function −
Example
const arrayOne = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; const doubleSum = arr => { const res = []; for(let i = 0; i < arr.length; i += 2){ res.push(arr[i] + (arr[i+1] || 0)); }; return res; }; console.log(doubleSum(arrayOne));
Output
The output in the console will be −
[ 1, 5, 9, 13, 17 ]
- Related Articles
- Return the sum of two consecutive elements from the original array in JavaScript
- JavaScript - Constructs a new array whose elements are the difference between consecutive elements of the input array
- How to add new array elements at the beginning of an array in JavaScript?
- Power array elements of an array with a given value and display the result in a different type in Numpy
- Divide numbers from two columns and display result in a new column with MySQL
- Consecutive elements sum array in JavaScript
- Counting the occurrences of JavaScript array elements and put in a new 2d array
- How to add two arrays into a new array in JavaScript?
- Return the floor of the array elements and store the result in a new location in Numpy
- Return the ceil of the array elements and store the result in a new location in Numpy
- How to subtract elements of two arrays and store the result as a positive array in JavaScript?
- Return the truncated value of the array elements and store the result in a new location in Numpy
- Accumulating array elements to form new array in JavaScript
- Compress array to group consecutive elements JavaScript
- Return Top two elements from array JavaScript

Advertisements