What is your hint or solution suggestion?
The first solution seems overly complicated. This solution uses the Array.reduce method to perform repeated, cumulative operations on elements of the array with different starting values.
The final values are returned in the object, along with a simple if-else inequality test.
Solution 2
function pythagoreanMeans(rangeArr) {
const n = rangeArr.length;
const A = rangeArr.reduce((a, b) => a+b, 0)/n;
const G = rangeArr.reduce((a, b) => a*b, 1)**(1/n);
const H = n/rangeArr.reduce((a, b) => a + (b**-1), 0);
if(A>=G && G>=H){
var result = 'is A >= G >= H ? yes'
}
else{
var result = 'is A >= G >= H ? no'
}
var final = {values: {
Arithmetic: A,
Geometric: G,
Harmonic: H
},test: result};
return final;
}
Challenge: Rosetta Code: Averages/Pythagorean means
Link to the challenge: