2
\$\begingroup\$

I have an array of values. (cols)

I want to find out which index of cols contains the value closest to the mean of cols.

Can I combine these functions so that I'm not repeating loops?

let mean = cols.reduce((prev, curr) => prev + curr) / cols.length;
let closest = cols.reduce((prev, curr) => (curr - mean) < (prev - mean) ? curr : prev);
let meanCol = cols.indexOf(closest);
\$\endgroup\$
3
  • \$\begingroup\$ Seems like you are calculating the average, not the mean; byjus.com/maths/difference-between-average-and-mean If you wanted to actually find closest to mean, then you need only 1 loop. \$\endgroup\$
    – konijn
    Mar 2, 2020 at 13:25
  • \$\begingroup\$ Hmmm... Interesting, I actually came across that link before, but because it says multiple times that 'they can be used interchangeably' I took that to mean they're the same thing! Also, I keep finding conflicting answers, such as this one saying that I am indeed doing mean: purplemath.com/modules/meanmode.htm \$\endgroup\$
    – aName
    Mar 2, 2020 at 14:47
  • 2
    \$\begingroup\$ Wikipedia agrees with you, so case closed for me. \$\endgroup\$
    – konijn
    Mar 2, 2020 at 16:21

2 Answers 2

3
\$\begingroup\$

You can remove the last line searching for the index since you can get the index in the reduce method.

Btw, you forgot to add Math.abs to deal with positive value when comparing.

Demo:

const cols = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

const mean = cols.reduce((prev, curr) => prev + curr, 0) / cols.length;

const { value, index } = cols.reduce((prev, curr, i) => {
    // You have deal in absolute 
    if (Math.abs(curr - mean) < Math.abs(prev.value - mean)) {
        return { value: curr, index: i }
    } else {
        return prev
    }
}, { value: cols[0], index: 0 });

console.log({ mean, value, index })

\$\endgroup\$
2
\$\begingroup\$

A short review;

  • You can't reduce an empty array, I would check for empty arrays and decide how to deal with them
  • Have a think about let vs. const, mean and closest should probably be const
  • You want that code in a properly named function
\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.