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
Calculating quarterly and yearly average through JavaScript
In this tutorial, we'll learn how to calculate quarterly and yearly averages from an array of numbers using JavaScript. This involves chunking data into groups and computing their averages.
Suppose we have an array of numbers like this:
const arr = [1,2,2,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
console.log("Original array:", arr);
Original array: [1, 2, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
We need to group this array into quarterly (3 elements) and yearly (12 elements) chunks, then calculate their averages.
Understanding the Grouping
The groups for the above array should look like this:
// Quarterly groups (every 3 elements)
const quarterly = [[1,2,2],[4,5,6],[7,8,9],[10,11,12],[13,14,15],[16,17,18],[19,20]];
// Yearly groups (every 12 elements)
const yearly = [[1,2,2,4,5,6,7,8,9,10,11,12],[13,14,15,16,17,18,19,20]];
console.log("Quarterly groups:", quarterly);
console.log("Yearly groups:", yearly);
Quarterly groups: [[1,2,2],[4,5,6],[7,8,9],[10,11,12],[13,14,15],[16,17,18],[19,20]] Yearly groups: [[1,2,2,4,5,6,7,8,9,10,11,12],[13,14,15,16,17,18,19,20]]
Implementation
Here's the complete function that groups data and calculates averages:
const arr = [1,2,2,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
const findAverages = arr => {
const quarterLength = 3, yearLength = 12;
const sumOfGroup = (arr, num) => {
return arr.reduce((acc, val, ind) => {
if (ind % num === 0){
acc.push(0);
};
acc[acc.length - 1] += val;
return acc;
}, []);
};
const quarters = sumOfGroup(arr, quarterLength);
const years = sumOfGroup(arr, yearLength);
return {
"yearlyAverage": years,
"quarterlyAverage": quarters
};
};
console.log(findAverages(arr));
{
yearlyAverage: [ 77, 132 ],
quarterlyAverage: [
5, 15, 24, 33,
42, 51, 39
]
}
How It Works
The sumOfGroup function uses reduce to group elements:
- When
ind % num === 0, it creates a new group by pushing 0 to the accumulator - Each value is added to the last group in the accumulator
- For quarterly: groups every 3 elements, for yearly: groups every 12 elements
Enhanced Version with True Averages
The above function returns sums, not averages. Here's an improved version that calculates actual averages:
const calculateAverages = arr => {
const quarterLength = 3, yearLength = 12;
const getAverages = (arr, groupSize) => {
const groups = [];
for (let i = 0; i < arr.length; i += groupSize) {
const group = arr.slice(i, i + groupSize);
const average = group.reduce((sum, val) => sum + val, 0) / group.length;
groups.push(Math.round(average * 100) / 100); // Round to 2 decimal places
}
return groups;
};
return {
quarterlyAverage: getAverages(arr, quarterLength),
yearlyAverage: getAverages(arr, yearLength)
};
};
const data = [1,2,2,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
console.log(calculateAverages(data));
{
quarterlyAverage: [ 1.67, 5, 8, 11, 14, 17, 19.5 ],
yearlyAverage: [ 6.42, 16.5 ]
}
Comparison
| Method | Output Type | Accuracy |
|---|---|---|
| Original (sumOfGroup) | Sums | Shows totals, not averages |
| Enhanced (getAverages) | True Averages | Accurate average calculations |
Conclusion
This approach efficiently groups array data into quarterly and yearly segments, then calculates their averages. The enhanced version provides true mathematical averages rather than just sums, making it more useful for data analysis.
