javascript
Parameters: array: Array of numbers
An array of numbers to calculate the average
Returns: Returns the average of array numbers
The findArrayAverage function is a simple tool used to calculate the average of a set of numbers in an array. It offers a practical way to gather general information from data sets, which can be applied in a range of programming fields.
Hello there, fellow programmer! In this blog post, we will be taking a close look at a common function in programming: findArrayAverage. This function is often used to calculate the average of an array of numbers. The steps outlined below will help you understand its inner workings, and how you can implement it yourself in JavaScript. The code snippets are easy to follow and are explained in detail. So put on your coding hat and let's dive in!
We are tasked with creating a function, findArrayAverage
, that takes an array of numbers as an input and returns the average of those numbers. In other words, this function must sum up all the numbers in the array, and then divide this sum by the number of elements in the array.
Let's start by defining a simple function, findArrayAverage
, which takes one parameter, numbers
. For now, this function won't do anything - it's an empty shell.
function findArrayAverage(numbers) {
}
The first part of our task is to calculate the sum of all the numbers in the array. We can accomplish this using the reduce
function in JavaScript.
The reduce
function applies a function to each item of the array, in order, so as to reduce the array to a single output value. In our case, this function should take two inputs (the current sum and the current number) and return their sum.
function findArrayAverage(numbers) {
let sum = numbers.reduce(function(a, b) {
return a + b;
}, 0);
}
Having obtained the sum of the array elements, the remaining task is simple. The average of a list of numbers is equal to the sum of those numbers, divided by the number of those numbers. We can get the number of items in our array using the length
property, and return the final computed average from the function.
function findArrayAverage(numbers) {
let sum = numbers.reduce(function(a, b) {
return a + b;
}, 0);
return sum / numbers.length;
}
Now our findArrayAverage
function is complete. This function can be used to find the average of any array of numbers.
Here's the complete function:
function findArrayAverage(numbers) {
let sum = numbers.reduce(function(a, b) {
return a + b;
}, 0);
return sum / numbers.length;
}
For example, calling findArrayAverage([1, 2, 3, 4, 5])
would return 3
(since that's the average of the numbers 1, 2, 3, 4, and 5).
With this function, calculating an array average is now an easy task!
The function applies the arithmetic mean principle. It calculates the sum of all numbers in the array, and then divides the sum by the quantity of numbers. It looks something like this: `average = (num1+num2+...+numN)/N`. The result will be the average of the numbers in the array.
Learn more