Question: Write a defining table and a function that returns the value of the middle element in an array. If the array has an even number
Write a defining table and a function 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. The function must have this header:
function getMiddle(list)
Use this code to test function:
function doCalcs()
{
var evenlist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var oddlist = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var endsums = addEnds(evenlist); //This should be 1 + 10 = 11
var evenmiddle = getMiddle(evenlist); //This should be 5 + 6 / 2 = 5.5
var oddmiddle = getMiddle(oddlist); //This should be 5
document.getElementById("output").innerHTML = "endsums = " + endsums + " ";
document.getElementById("output").innerHTML += "evenmiddle = " + evenmiddle + " ";
document.getElementById("output").innerHTML += "oddmiddle = " + oddmiddle + " ";
}
function addEnds(list)
{
//Put your code here
}
function getMiddle(list)
{
//Put your code here
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
