Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

134
Views
Find the middle element in an array

Please help me write a function that will find the middle element of an array according to the following rule:

['a','b','c'] => 'b'
['a','b','c','d'] => 'b'
['a','b','c','d','e'] => 'c'

extreme cases:

[] => null
['a'] => 'a'
[null] => undefined
['a',null,'c'] => 'a'
['a','b','c',null, null] => 'b'

Here's what I was able to do, but it doesn't work quite right:

function findMidlle(arr)
{
    if(arr.length % 2 == 1) {
        return arr[(arr.length-1)/2];
    } else {
        return (arr[(arr.length/2)]+arr[(arr.length/2 -1)])/2
    }
}

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Options

  • Math.floor((arr.length - 1) / 2) to always have the element at the center (or before)
  • Math.floor(arr.length / 2) to always have the element at the center (or after)

Example

function middle(arr) {
    return arr.length ? arr[Math.floor((arr.length - 1) / 2)] : undefined;
}

function middle(arr) {
    return arr.length ? arr[Math.floor((arr.length - 1) / 2)] : undefined;
}

console.log(middle(['a','b','c']));
console.log(middle(['a','b','c','d']));
console.log(middle(['a','b','c', 'd', 'e']));

Solution

In your specific case you are requesting some additional conditions:

function middle(arr) {
    return arr.length ? arr[Math.floor((arr.length - 1) / 2)] : undefined;
}

function specialMiddle(arr) {
    if (!arr.length) return null;
    if (arr.length === 1 && arr[0] === undefined) return undefined;
    else return middle(arr.filter(a => a !== null /*&& a !== undefined*/));
}

console.log(specialMiddle([]));
console.log(specialMiddle(['a']));
console.log(specialMiddle([null]));
console.log(specialMiddle(['a', null, 'c']));
console.log(specialMiddle(['a', 'b', 'c', null, null]));

about 4 years ago · Juan Pablo Isaza Report

0

You have quite an interesting set of particular cases, especially the ones:

[] => null
[null] => undefined

And from the others I get that you need to start by filtering the array

['a',null,'c'] => 'a'
['a','b','c',null, null] => 'b'

So here's my try:

function findMiddle(arr) {
  if (arr.length == 0) return null;
  const filteredArr = arr.filter(x => x);
  const len = filteredArr.length;
  
  return (len == 0)?undefined
         : (len%2 == 0)?filteredArr[(len/2)-1]
         : filteredArr[Math.floor(len/2)]
 
}


function easyLog( shouldBe, itIs ) {
  console.log("sould be: ",shouldBe," it is: ",itIs);
}


// ['a','b','c'] => 'b'
easyLog('b',findMiddle(   ['a','b','c']   ));
//['a','b','c','d'] => 'b'
easyLog('b',findMiddle(   ['a','b','c','d']   ));
//['a','b','c','d','e'] => 'c'
easyLog('c',findMiddle(  ['a','b','c','d','e']    ));
//extreme cases:

//[] => null
easyLog('null',findMiddle([]));
//['a'] => 'a'
easyLog('a',findMiddle(   ['a']   ));
//[null] => undefined
easyLog('undefined',findMiddle([null]));
//['a',null,'c'] => 'a'
easyLog('a',findMiddle(   ['a',null,'c']   ));
//['a','b','c',null, null] => 'b'
easyLog('b',findMiddle(   ['a','b','c',null, null]   ));

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!