Additional solution for Rosetta Code: Averages/Pythagorean means

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:

Looks good to me. I only have some minor style suggestions

Clickity Clackity
function pythagoreanMeans(rangeArr) {
  // Compute means
  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);

  // Compare means
  if (A >= G && G >= H) {
    let result = 'is A >= G >= H ? yes';
  } else {
    let result = 'is A >= G >= H ? no';
  }

  // Create and return object
  return {
    values: {
      Arithmetic: A,
      Geometric: G,
      Harmonic: H
    },
    test: result,
  };
}
1 Like

I agree, this makes it look better

1 Like