7

I'm trying to think of a way to make this code simple, with the smallest amount of loops and variables, but I'm having trouble.

I want to get the average object in the array 'numbers', based on the 'value'. I feel that there must be a mathematical way to get the average without finding the closest average in another loop.

Currently I have this mess:

var numbers = [
	{ value: 41 },
	{ value: 19 },
	{ value: 51 },
	{ value: 31 },
	{ value: 11 }
];
// Find average:
var sum = 0;
for (var i = 0; i < numbers.length; i++) {
	sum += numbers[i].value;
}
var average = sum / numbers.length;
// Find closest object to average:
var match, difference;
for (var j = 0; j < numbers.length; j++) {
	const diff = Math.abs(average - numbers[j].value);
	if (difference === undefined || diff < difference) {
		difference = diff;
		match = numbers[j];
	}
}
// Print junk:
console.log("AVERAGE NUMBER: " + average);
console.log("CLOSEST OBJECT: " + match);
console.log("CLOSEST NUMBER: " + match.value);

I need to retrieve the object because it contains other information that I need to use.

Any help would be highly appreciated!

3
  • 4
    I'm voting to close this question as off-topic because "less horrible" is very subjective. Questions about working code are usually a better fit for codereview.stackexchange.com
    – Quentin
    May 11, 2018 at 8:43
  • @Quentin sorry, I'll try to rewrite May 11, 2018 at 8:45
  • I was having a hard time wording the question, so the code above was written to help explain the problem, but it seems to be the solution May 11, 2018 at 9:46

1 Answer 1

7

At least you need two loops for getting the average and the closest item, because you need to visit all items in the first run and you do not have the possibillity to store in advance which item has the closest value.

You could get first the average and then reduce the closest value with the object.

var numbers = [{ value: 41 }, { value: 19 }, { value: 51 }, { value: 31 }, { value: 11 }, { value: 30 }],
    average = numbers.reduce((sum, { value }) => sum + value, 0) / numbers.length,
    closest = numbers.reduce((a, b) =>
        Math.abs(average - a.value) <= Math.abs(average - b.value)
            ? a
            : b
    );

console.log(average);
console.log(closest);

7
  • If I add 30 to the mix, the averate is 30.5, and the closest is given as 30. Should it not be 31?
    – mplungjan
    May 11, 2018 at 8:50
  • @mplungjan in this case, which result do you prefer? May 11, 2018 at 8:52
  • @NinaScholz thank you for this! I was just wondering, do you think it is mathematically possible to find the average object without first finding the average, then the closest average? I feel that I am doing something wrong using two loops. Is a simpler way possible? May 11, 2018 at 8:54
  • I would expect 31 for 30.5+
    – mplungjan
    May 11, 2018 at 8:55
  • @MysteryPancake are you thinking about the median? You can't find the average without finding the average. You also can't find the closest to something you do not know yet either. Or am I misinterpreting what you are saying?
    – mTv
    May 11, 2018 at 9:04

Your Answer

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

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