How to resolve the algorithm Averages/Arithmetic mean step by step in the JavaScript programming language
How to resolve the algorithm Averages/Arithmetic mean step by step in the JavaScript programming language
Table of Contents
Problem Statement
Write a program to find the mean (arithmetic average) of a numeric vector. In case of a zero-length input, since the mean of an empty set of numbers is ill-defined, the program may choose to behave in any way it deems appropriate, though if the programming language has an established convention for conveying math errors or undefined values, it's preferable to follow it.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Averages/Arithmetic mean step by step in the JavaScript programming language
Both responses provide detailed explanations of the provided JavaScript code examples that calculate the mean (average) of an array of numbers. They cover the different approaches used in each example, including iterative loops, the forEach
method, the reduce
method, and a custom mean
method added to the Array
prototype.
Here's a breakdown of each example:
1. Iterative Loop:
function mean(array) {
var sum = 0, i;
for (i = 0; i < array.length; i++) {
sum += array[i];
}
return array.length ? sum / array.length : 0;
}
This function uses a loop to iterate through the array, adding each element to the sum. It checks if the array has any elements (i.e., if array.length
is not zero) and returns the mean (sum divided by length) if there are elements; otherwise, it returns 0.
2. forEach
Method:
function mean(array) {
var sum = 0;
array.forEach(function(value) {
sum += value;
});
return array.length ? sum / array.length : 0;
}
This function uses the forEach
method to iterate through the array, adding each element to the sum. Similar to the previous example, it checks the array length and returns the mean or 0 accordingly.
3. reduce
Method:
function mean(array) {
return !array.length ? 0
: array.reduce(function(pre, cur, i) {
return (pre * i + cur) / (i + 1);
});
}
This function uses the reduce
method to calculate the mean. The pre
parameter represents the previous value (sum so far), cur
is the current element, and i
is the current index. The expression (pre * i + cur) / (i + 1)
calculates the running mean as it iterates through the array. Again, it checks the array length and returns the mean or 0 as appropriate.
4. Custom mean
Method on Array
Prototype:
Array.prototype.mean = function() {
return !this.length ? 0
: this.reduce(function(pre, cur, i) {
return (pre * i + cur) / (i + 1);
});
}
This code adds a custom mean
method to the Array
prototype. It behaves similarly to the previous example, but it allows you to call the mean
method directly on an array, simplifying the syntax.
5. Functional Programming Approach with Functional.reduce
:
function mean(a) {
return a.length ? Functional.reduce('+', 0, a) / a.length : 0;
}
This function uses the Functional.reduce
function to calculate the mean. The '+'
parameter represents the addition operator, the 0
is the initial value, and the a
parameter is the array. The expression Functional.reduce('+', 0, a) / a.length
calculates the sum and divides it by the array length to get the mean.
6. Arrow Function with Ternary Operator:
(sample => {
// mean :: [Num] => (Num | NaN)
let mean = lst => {
let lng = lst.length;
return lng ? (
lst.reduce((a, b) => a + b, 0) / lng
) : NaN;
};
return mean(sample);
})([1, 2, 3, 4, 5, 6, 7, 8, 9]);
This function uses an arrow function (lst => ...
) and a ternary operator (lng ? ... : NaN
) to calculate the mean. It checks if the list has any elements (i.e., if lng
is not zero) and returns the mean (sum divided by length) or NaN
(not a number) if the list is empty.
In summary, these code examples demonstrate different ways to calculate the mean of an array in JavaScript. The choice of approach depends on the specific requirements and preferences of the developer.
Source code in the javascript programming language
function mean(array)
{
var sum = 0, i;
for (i = 0; i < array.length; i++)
{
sum += array[i];
}
return array.length ? sum / array.length : 0;
}
alert( mean( [1,2,3,4,5] ) ); // 3
alert( mean( [] ) ); // 0
function mean(array) {
var sum = 0;
array.forEach(function(value){
sum += value;
});
return array.length ? sum / array.length : 0;
}
alert( mean( [1,2,3,4,5] ) ); // 3
function mean(array) {
return !array.length ? 0
: array.reduce(function(pre, cur, i) {
return (pre * i + cur) / (i + 1);
});
}
alert( mean( [1,2,3,4,5] ) ); // 3
alert( mean( [] ) ); // 0
Array.prototype.mean = function() {
return !this.length ? 0
: this.reduce(function(pre, cur, i) {
return (pre * i + cur) / (i + 1);
});
}
alert( [1,2,3,4,5].mean() ); // 3
alert( [].mean() ); // 0
function mean(a)
{
return a.length ? Functional.reduce('+', 0, a) / a.length : 0;
}
(sample => {
// mean :: [Num] => (Num | NaN)
let mean = lst => {
let lng = lst.length;
return lng ? (
lst.reduce((a, b) => a + b, 0) / lng
) : NaN;
};
return mean(sample);
})([1, 2, 3, 4, 5, 6, 7, 8, 9]);
5
You may also check:How to resolve the algorithm Modular arithmetic step by step in the Python programming language
You may also check:How to resolve the algorithm URL decoding step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Run-length encoding step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm A+B step by step in the Insitux programming language
You may also check:How to resolve the algorithm Strip control codes and extended characters from a string step by step in the Raku programming language