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>     

6
  • 1
    The code looks like it has been cut and pasted without any attempt to make it run. Commented Mar 28, 2016 at 0:29
  • where's binarySearch? Commented Mar 28, 2016 at 0:32
  • There are multiple SO questions/answers about getting the median of a JS array. Commented Mar 28, 2016 at 0: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. Commented Mar 28, 2016 at 0: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. Commented Mar 28, 2016 at 0:37

3 Answers 3

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;
}
Sign up to request clarification or add additional context in comments.

Comments

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>

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.