Get the middle two items of an array with even number of items

Viewed 3032

So I need to return the middle item of an array (if there is odd number of items), pretty easy:

function middleItem(oddArr) {
  let count = 0;
  if (arr.length % 2 !== 0) {
    for (i = 0; i < arr.length; i++) {
      arr[i] === arr[(arr.length - 1) / 2] && (count = arr[i]);
      }
    }
  return count
  }

But when it comes to return the middle two items from an array with even number of items, I start lose my mind lol. I search for 2 hours without looking for an answer (I need to improve my logic) but now I would love to know what you guys think is the best method to do it. Many thanks !

function middleItem(evenArr) {

}
4 Answers

The logic is pretty easy:

  • If array is empty, don't return anything
  • If array has odd number of items, the index of the middle item is simply Math.floor(arr.length / 2)
  • If array has even number of items, the index of the two middle items are arr.length / 2 - 1 and arr.length / 2 respectively

See proof-of-concept example:

function middleItem(arr) {
  if (!arr.length)
    return;
    
  const mid = arr.length / 2;
  if (arr.length % 2 === 1) {
    return arr[Math.floor(mid)];
  } else {
    return [
      arr[mid - 1],
      arr[mid],
    ];
  }
}

console.log(middleItem([]));            // undefined
console.log(middleItem([0]));           // 0
console.log(middleItem([0,1]));         // [0, 1]
console.log(middleItem([0,1,2]));       // 1
console.log(middleItem([0,1,2,3]));     // [1, 2]
console.log(middleItem([0,1,2,3,4]));   // 2

You could ise a bishifting which returns an integer value an check if the length is odd, then take the middle index otherwise slice the array and get the two middle elements.

function getMiddle(array) {
    const middle = array.length >> 1;
    return array.length & 1
        ? array[middle]
        : array.slice(middle - 1, middle + 1);
}

console.log(getMiddle([1, 2, 3, 4, 5]));
console.log(getMiddle([1, 2, 3, 4, 5, 6]));

To get the middle value from an array of odd numbers:

const middle = Math.floor(arr.length / 2);

To get the middle 2 values from an array of even numbers:

const midPoint = arr.length / 2;
const middle2 = [arr[midPoint - 1], arr[midPoint]];

I think that Math.floor and Math.ceil can be used - BUT even numbers must be made into odd numbers to get two different values. Thus - Math.floor(4/2) and Math.ceil(4/2) would both return 2, but Math.floor((4+1)/2) and Math.ceil((4+1)/2) would return 2 and 3. If the number is odd, we are back to both values being the same. Thus, we can code for that and slice the array:

let arr = [1, 2, 3, 4, 5, 6];
let arr2 = [1, 2, 3, 4, 5];
let arr3 = [1, 2, 3, 4, 5, 6, 8, 10, 12, 14];
let arr4 = [1, 2, 3, 4, 5, 7, 0];
let arr5 = [1, 2, 3, 4, 5, 3];


function middleItem(a) {
  let middle1 = Math.floor((a.length + 1) / 2);
  let middle2 = Math.ceil((a.length + 1) / 2);
  return a.slice(middle1 - 1, middle2);
}

console.log(arr + " > " + middleItem(arr));
console.log(arr2 + " > " + middleItem(arr2));
console.log(arr3 + " > " + middleItem(arr3));
console.log(arr4 + " > " + middleItem(arr4));
console.log(arr5 + " > " + middleItem(arr5));

Related