2015-12-03 56 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