Looking to develop a function that can shrink a list of numbers based on a specified variation.
Variation Explanation:
The variation should cover a range above and below a given value. For example, if the variance is set at 100 for a value of 5460, it should encompass any number between 5410 - 5510 (50 below and 50 above).
For instance, when presented with this array:
[ 1576420754, 1576420756, 1576593554, 1581172759, 1581172764 ]
I wish to create a function filterSimilarValues(array, variance = 100)
This function should produce the following result:
[ 1576420756, 1576593554, 1581172764 ]
I've experimented with different approaches such as
const filterSimalarValues = (array, variance = 100) => {
let filteredArray = [];
for (let i = 0; i < array.length; i++) {
const number = array[i];
if (number >= number - (variance / 2) && number <= number + (variance / 2)) {
return;
}
filteredArray.push(number);
}
return filteredArray;
};