
- 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
Cumulative average of pair of elements in JavaScript
We have an array of numbers and are required to write a function that returns an array with the average of the corresponding element and its predecessor.
For the first element, as there are no predecessors, so that very element should be returned.
Let’s write the code for this function, we will use the Array.prototype.map() function to solve this problem.
Example
The code for this will be −
const arr = [3, 5, 7, 8, 3, 5, 7, 4, 2, 8, 4, 2, 1]; const consecutiveAverage = arr => { return arr.map((el, ind, array) => { const first = (array[ind-1] || 0); const second = (1 + !!ind); return ((el + first) / second); }); }; console.log(consecutiveAverage(arr));
Output
The output in the console will be −
[ 3, 4, 6, 7.5, 5.5, 4, 6, 5.5, 3, 5, 6, 3, 1.5 ]
- Related Articles
- Cumulative sum of elements in JavaScript
- Determine whether there is a pair of values in the array where the average of the pair equals to the target average in JavaScript
- Pair of similar elements at different indices in JavaScript
- Get average of every group of n elements in an array JavaScript
- Find consecutive elements average JavaScript
- Retaining array elements greater than cumulative sum using reduce() in JavaScript
- Pair of (adjacent) elements of an array whose sum is lowest JavaScript
- Excluding extreme elements from average calculation in JavaScript
- Find the average of all elements of array except the largest and smallest - JavaScript
- Converting array of Numbers to cumulative sum array in JavaScript
- Greatest sum of average of partitions in JavaScript
- Check Average of Odd Elements or Even Elements are Greater in Java
- Return the cumulative sum of array elements treating NaNs as zero in Python
- Return the cumulative product of array elements treating NaNs as one in Python
- Calculating average of an array in JavaScript

Advertisements