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