TheRoyality
TheRoyality

Reputation: 113

Find two values in array where a given number is between

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?

Upvotes: 1

Views: 1142

Answers (5)

Aalexander
Aalexander

Reputation: 5004

Assumption

  • The array is sorted.

The Condition

  • Check if it is smaller then the current element in the array and greater then the next element in the array.

Note: If the array isn't sorted you can do Array.sort()

const arr = [200, 180, 150, 120, 80];

let sum = arr.reduce((acc, x) => {
  return acc + x
}, 0)

let avg = sum / arr.length;

for (let i = 0; i < arr.length - 1; i++) {

  if (avg < arr[i] && avg > arr[i + 1]) {
    console.log("Here at indice " + (i + 1) + " is " + avg + "'s place");
  }

}

Upvotes: 2

pablop76
pablop76

Reputation: 1

const array = [200, 180, 150, 120, 80];
    
    const check = (array,a,b)=>{
    
      //copy array
      const arrayClone = [...array];
      //sum copy array
      const sum = arrayClone.reduce(function(a, b){
          return a + b;
      }, 0);
      //average copy array
      const average = sum / arrayClone.length;
    
      //adding to an copy array
      arrayClone.push(average);
    
      //sort copy array
      arrayClone.sort(function(a, b) {
        return b-a;
      });
      // take the index of the push number
      const index = arrayClone.indexOf(average);
    
      // check that the average number is between the two numbers given
      if((arrayClone[index-1]===a)&&(arrayClone[index+1]===b)){
        // if ok
        console.log(arrayClone.slice(index-1,index+2))
      }else{
        console.log("not found")
      }
    
    }
    
    check(array,150,120);

Upvotes: 0

MM2021
MM2021

Reputation: 150

    let numArr = [200, 180, 150, 120, 80];
    
    const calcAvg = arr => arr.reduce((acc, cur) => acc += cur, 0) / numArr.length;
    
    const isAvgPlace = (arr, start, end, avg) => {
        let sorted = [...arr].sort((a,b) => a - b);
        let avgPlace = sorted.filter((cur, i) => avg <= cur && sorted[i+1] >= avg);
        return avgPlace[0] === start && avgPlace[1] === end;
    }

    const average = calcAvg(numArr);
    console.log(isAvgPlace(numArr, 150, 180, average));
    console.log(isAvgPlace(numArr, 120, 150, average));

P.S: Please use meaningful names for naming variables, function and etc. Using generic names like array and check decreases readability and is a bad practice.

Upvotes: 0

sbgib
sbgib

Reputation: 5828

Try like this:

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, start, end, value) => {
  let istart = array.indexOf(start);
  let iend = array.indexOf(end);
  let iaverage = 0;

  if (istart === -1 || iend === -1) {
    return null;
  };

  for (var i = istart; i <= iend; i++) {
    if (arr[i] <= average) {
      return i;
    };
  };
  
  return null;
};

console.log('check', check(array, 150, 120, average)); // 120 and 150 must be found in function as these values can't be hardcoded and will change

The simplest way would be to loop between the positions of start and end and check for where average would fit.

Upvotes: 0

Siva Kondapi Venkata
Siva Kondapi Venkata

Reputation: 11001

Use findIndex method. (inline comments)

const getIndexesBound = (arr, avg) => {
  const index = arr.findIndex((num) => avg > num);
  if (index < 1) {
    // when index 0 means first element
    // when index -1, can not find num
    return "Can not find indexes bound";
  } else {
    return [index - 1, index];
  }
};


const arr = [200, 180, 150, 120, 80];

console.log('Bound indexes for 146', getIndexesBound(arr, 146));

Upvotes: 0

Related Questions