0

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>     
Giles
  • 113
  • 2
  • 3
  • 11
  • 1
    The code looks like it has been cut and pasted without any attempt to make it run. – user2182349 Mar 28 '16 at 00:29
  • where's `binarySearch`? – Daniel A. White Mar 28 '16 at 00:32
  • There are multiple SO questions/answers about getting the median of a JS array. – Bryan Lewis Mar 28 '16 at 00:34
  • This has little to do with arrays, and mostly about simple math and logic. Take a clean piece of paper, write down a few test cases for arrays of variable lengths, mark the answer you want from each case and pay close attention to the indexes of each answer. Then try again. – Amit Mar 28 '16 at 00:35
  • oh whoops i changed that, i was trying to use binary search to find the middle element. – Giles Mar 28 '16 at 00:35
  • 1
    `list.length / 2` gets you the middle element, and logic should tell you that if the array is even, you get `list[list.length/2]` and the one before it (`(list.length/2) - 1`), add them up, and divide by two. – adeneo Mar 28 '16 at 00:37

3 Answers3

1

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;
}
Community
  • 1
  • 1
Ali
  • 558
  • 7
  • 28
0

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>
A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76
0
var idx = (testNumbers.length-1) / 2;
document.getElementById('outputDiv1').textContent =
  ( testNumbers[Math.floor(idx)] + testNumbers[Math.ceil(idx)] )/2;
Pinke Helga
  • 6,378
  • 2
  • 22
  • 42