Imagine having an array of data structured like this:
var data = [{name: "craig", value: 10}, {name: "oliver", value: 15}]
My goal is to create a function that can accept parameters like:
function findExtremeValue(windowSize, pointsToTake, data, valueAccessor) {}
Where windowSize represents the number of positions in the array to consider, and pointsToTake is the desired number of data points to be returned.
To achieve this, I need to calculate the mean by obtaining the sum of all values. Then, for each array position, I must find the absolute distance from the mean and identify which data point is farthest from the mean. This data point should then be added to a new array.
Here is the initial code I have:
var data = [{name: "craig", value: 10}, {name: "oliver", value: -10}]
function findExtremeValue(windowSize, pointsToTake, data, valueAccessor) {
var i;
var sum = 0;
for(i = 0; i < windowSize; i++) {
sum += valueAccessor(data[i]);
}
var mean = sum/windowSize;
for (i = 0; i < windowSize; i++) {
Math.abs(valueAccessor(data[i]) - mean);
}
}
findExtremeValue(5, 1, data, function(item) { return item.value });
My question is, how should I modify the findExtremeValue function in order to identify the array position with the data point that is farthest from the mean, and then add that data point to a new array?
Thank you in advance for your help, and I hope this explanation is clear.