Giles
Giles

Reputation: 113

finding Middle element of an array in javascript

I need to write a program that finds the middle of an array and returns the value stored there unless the array is even then it should return the average of the two middle most numbers. Here is the code i have so far. i'm stuck on how i would find the middle two numbers in an even array and return the average. I'm a super beginner in java script so all help is appreciated. Thanks!

<!DOCTYPE HTML>
<html lang="en-us">
<head>
<meta charset="utf-8">
<title>Add Ends</title>
    <script language="javascript" type="text/javascript">
      /*
      Write a function named getMiddle that returns the value of the middle element in an array. If the array has an even number of elements, then this function must return the average of the two middle elements.
      */
        var testNumbers = [0, 1 ,2, 3, 4, 5, 6, 7, 8, 9]


       function isEven()
        {
        var mid = (testNumbers[0] + (testNumbers.length)) / 2;   
        }
        
        function getMiddle(list) 
        {
            var mid = (testNumbers[0] + (testNumbers.length)) / 2;
            if (mid % 2 == 0)
                {
                var evenMid = isEven();
                document.getElementById("outputDiv1").innerHTML = evenMid;
                }
            else 
                {
                document.getElementById("outputDiv1").innerHTML = mid;
                }
        }

    </script>   
</head>

<body>
        <button type="button" onclick="binarySearch()">Find the Middle</button>
        <br>
        <div id="outputDiv1"></div>
</body>
</html>     

Upvotes: 0

Views: 6160

Answers (3)

Pinke Helga
Pinke Helga

Reputation: 6682

var idx = (testNumbers.length-1) / 2;
document.getElementById('outputDiv1').textContent =
  ( testNumbers[Math.floor(idx)] + testNumbers[Math.ceil(idx)] )/2;

Upvotes: 0

A.J. Uppal
A.J. Uppal

Reputation: 19264

Try the following:

/*
      Write a function named getMiddle that returns the value of the middle element in an array. If the array has an even number of elements, then this function must return the average of the two middle elements.
      */
        var testNumbers = [0, 1 ,2, 3, 4, 5, 6, 7, 8, 9]

        function output(){
            var mid = getMiddle(JSON.parse(document.getElementById("lst").value));
            outputDiv1.innerHTML = mid;
        }
        
        function getMiddle(list) 
        {
            if(list.length % 2 == 1){
                return list[(list.length-1)/2];
            }else{
                return (list[(list.length/2)]+list[(list.length/2 -1)])/2
            }
        }
<!DOCTYPE HTML>
<html lang="en-us">
<head>
<meta charset="utf-8">
<title>Add Ends</title>
</head>

<body>
        <input id="lst">
  <button type="button" onclick="output()">Find the Middle</button>
        <br>
        <div id="outputDiv1"></div>
</body>
</html>

Upvotes: 0

Ali
Ali

Reputation: 558

This should get you somewhere (from this SO answer):

if (nums.length %2 == 0) {
    // even-length array (two middle elements)
    var avg = (nums[(nums.length/2) - 1] + nums[nums.length/2]) /2;
}

Upvotes: 1

Related Questions