2015-12-03 2 views
0

Мне нужно найти сумму элементов в концах массива. Также мне нужно найти среднее число элементов массива, если в списке элементов нечетно отображается среднее число. Если для списка элементов требуется даже среднее значение двух средних чисел. Я долгое время был в этом, и я больше не могу думать.JavaScript Массивы и циклы математика

 <!DOCTYPE HTML> 
     <html lang="en-us"> 
     <meta charset="utf-8"> 

     <head> 
<title>Functions With Arrays</title> 
<script type="text/javascript"> 
    /*Write a defining table and a function that returns the sum of the first and last values in an array. The function must have this header: function addEnds(list)*/ 

    /*Input: No input from user. 
     Process: Calls the addEnds function to add the ends of the array. 
     Output: Displays the sum of the array ends. 
    */ 

    // This function calls the addEnds function and the getMiddle function. 
    function multipleFunctions() { 
     var list = ["10", "20", "30", "40", "50"]; 
     var result = 0; 
     var result2 = 0; 
     var result3 = 0; 
     result = addEnds(list); 
     result2 = getMiddle(list); 
     result3 = "The sum of the array ends is " + result + "." + "<br>" + "The middle value of the array is " + result2 + "."; 
     document.getElementById("outputDiv1").innerHTML = result; 
    } 
    // This function adds the ends of the array index values. 
    function addEnds(list) { 
     var sum = list[0] + list[4]; 
     return sum; 
    } 

    /*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)*/ 
    function getMiddle(list) { 
     var middle = list[x];//don't know how... 
      var evenOrOdd = list % 2; 
     // Odd number of elements. 
     if (list !== 0) 
      return list[middle]; 
    } 
    // Even number of elements. 
    if (list === 0) 
    return list[x] + list[x]/2;//don't know how... 
    } 
</script> 
</head> 

<body> 
<h1> Array list: 10, 20, 30, 40, 50</h1> 
<h2>Click the compute function button to return the sum of the first and last values in the array list.<br> It will also return the average of the two middle elements of the array list.</h2> 
<button type="button" onclick="multipleFunction()">Compute Functions</button> 
<div id="outputDiv1"></div> 
</body> 

</html> 
+0

Есть целый ряд синтаксических проблем в коде –

+0

Кроме того, поскольку у вас есть массив строковых значений, то Оператор добавления (+) будет работать как оператор конкатенации –

+0

http://jsfiddle.net/arunpjohny/oL70e50v/1/ –

ответ

0

отпускает в пути, я исправил синтаксис и логические проблемы в коде

// This function calls the addEnds function and the getMiddle function. 
    function multipleFunctions() { 
     var list = [10, 20, 30, 40, 50]; //numeric array 
     var result = 0; 
     var result2 = 0; 
     var result3 = 0; 
     result = addEnds(list); 
     result2 = getMiddle(list); 
     result3 = "The sum of the array ends is " + result + "." + "<br>" + "The middle value of the array is " + result2 + "."; 
     document.getElementById("outputDiv1").innerHTML = result; 
    } 
    // This function adds the ends of the array index values. 
    function addEnds(list) { 
     var sum = list[0] + list[list.length -1]; 
     return sum; 
    } 

    /*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)*/ 
    function getMiddle(list) { 

     if(list.length % 2 === 0){ 
      var middle = list.length /2; 
      return (list[middle] + list[middle -1])/2; 
     } 
     else{ 

      return list[Math.round((list.length - 1)/2)]; 
     } 
    } 
+0

Он работал отлично !!! Спасибо, я изучу это много. – user5500799

Смежные вопросы