I have an array with numbers, but they can be unique and the numbers will change, but for now, let's assume it is like this:
const arr = [200, 180, 150, 120, 80];
And I have a number that is average from the array:
const average = 146;
I need a function that checks where this average number needs to be between two numbers from the array, in this case, between 150 and 120, and only then show something, etc. How can I achieve this?
My current progress, but this does not give the output I need:
let array = [200, 180, 150, 120, 80];
const sum = array.reduce(function(a, b){
return a + b;
}, 0);
const average = sum / array.length; // 146
let check = (arr: any[], start: number, end: number, value: number) => (
start <= end ?
arr.filter((_, i) => i >= start && i <= end) :
arr.filter((_, i) => i >= start || i <= end)
).includes(value);
console.log('check', check(array, 120, 150, average)); // 120 and 150 must be found in function as these values can't be hardcoded and will change
The desired outcome: if the average number is between these two numbers, then show this text etc. How can this be done?