- 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

# Sum of consecutive numbers in JavaScript

Let’s say, we have to write a function that takes in an array and returns another array in which the consecutive similar numbers are added up together.

For example −

const array = [1, 5, 5, 5, 8, 8, 9, 1, 4, 4, 2];

The output should be −

[1, 15, 16, 9, 1, 8, 2]

All consecutive 5s added up to 15, then 2 consecutive 8s added up to 16 similarly 4s added up to 8.

Therefore, let’s write the code for this function. We will use the Array.prototype.reduce() method here to reduce the original array and simultaneously construct a new one.

## Example

const array = [1, 5, 5, 5, 8, 8, 9, 1, 4, 4, 2]; const sumConsecutive = (array) => { return array.reduce((acc, val) => { if (acc.last === val) { acc.arr[acc.arr.length - 1] += val; } else { acc.arr.push(val); acc.last = val; } return acc; }, { arr: [], last: undefined }).arr; }; console.log(sumConsecutive(array));

## Output

The output in the console will be −

[ 1, 15, 16, 9, 1, 8, 2 ]

- Related Articles
- Consecutive Numbers Sum in C++
- Consecutive elements sum array in JavaScript
- The sum of two consecutive numbers are 45 find the numbers.
- Check three consecutive numbers - JavaScript
- N consecutive odd numbers JavaScript
- Express the following numbers as the sum of consecutive odd numbers: $36$.
- Maximum sum of n consecutive elements of array in JavaScript
- Python program for sum of consecutive numbers with overlapping in lists
- JavaScript to check consecutive numbers in array?
- Finding three desired consecutive numbers in JavaScript
- Print all possible sums of consecutive numbers with sum N in C++
- Three strictly increasing numbers (consecutive or non-consecutive). in an array in JavaScript
- Length of the longest possible consecutive sequence of numbers in JavaScript
- The sum of the squares of two consecutive even numbers is 340. Find the numbers.
- The sum of the squares of three consecutive natural numbers is 149. Find the numbers.

Advertisements