计算数字的平均值

时间:2017-09-16 08:07:25

标签: javascript html

if (isset($_POST['print_btn']) && $_POST['print_btn'] == "print") {

    //your code

}

一切看起来像我的代码在我的JavaScript第33行所期望的'函数calculateAverage'似乎无法正常工作,当我点击按钮来计算平均值我得到'NaN'作为我的结果,我我不确定如何解决这个问题,所以非常感谢你的帮助,谢谢

2 个答案:

答案 0 :(得分:1)

与数组numbers中的项目数相比,您正在迭代数字1次迭代。将其更新为:for (var i = 0; i < numbers.length; i++) {。您遇到问题的原因是:当您访问numbers[numbers.length]时,该值为undefined,而undefined/numbers.length为NaN。

而且,您需要在总数中添加新数字:total += numbers[i];

/*
   JavaScript used with 'average.html'
   Use an interactive debugger to find the errors in the JavaScript below
*/

"use strict";
//Global variables accessible to all functions
var numbers = []; //create an empty array
var enterButton = null; //global variables must be initialised
var calculateButton = null;

/* Get a number from the prompt
 *  If a valid number add to array, if not give error message to user
 *  Display the updated array of numbers on the web page
 *  Make the Calculate button visible
 */
function enterNumber() {
  var number = prompt("Enter your number");
  var number = Number(number); //WHAT DOES THIS LINE DO?
  if (number >= 0) { // test what is entered is a number   
    numbers.push(number); //add the number entered to the end of the array 
  } else {
    alert("Please enter a valid number");
  }
  document.getElementById("numberList").innerHTML = "The numbers you have entered so far are: " + numbers; //diplay a list of number entered
  enterButton.textContent = "Enter your next number"; //change the label on the Enter Button
  calculateButton.style.visibility = "visible"; //show the button  - uses the CSS property of the elenment
}
/* Calculate the sum and average of the array of number
 *  Display the results on the web page
 */
function calculateAverage() {
  var average = 0;
  var total = 0;
  for (var i = 0; i < numbers.length; i++) {
    total += numbers[i]; //add the each number in the array to the cumulative total 
  }
  average = total / i;
  document.getElementById("result").innerHTML = "The total of your numbers is " + total + " and their average is " + average;
}

function init() {
  enterButton = document.getElementById("enter");
  calculateButton = document.getElementById("calculate");
  calculateButton.style.visibility = "hidden"; //hide the Calculate button until some numbers are entered
  enterButton.onclick = enterNumber;
  calculateButton.onclick = calculateAverage;
}

window.onload = init;
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8" />
  <meta name="description" content="Demonstrates some logic errors" />
  <meta name="keywords" content="HTML, average, errors" />
  <title>Average numbers</title>
  <script src="badaverage.js"></script>
</head>

<body>
  <h1>Average numbers</h1>
  <p>Click the button to enter a series of numbers to average</p>
  <button id="enter">Enter your first number</button>
  <p><span id="numberList"></span></p>
  <button id="calculate">Calculate the average</button>
  <p><span id="result"></span></p>

</body>

</html>

答案 1 :(得分:0)

所做的更改:

  1. 在循环中将i<=numbers.length更改为i<numbers.length
  2. 更新了calculateAverage功能

    function calculateAverage(){
        var average = 0;
        var total = 0;
        for (var i = 0 ; i < numbers.length; i++){    
            total += numbers[i];   //add the each number in the array to the cumulative total 
        }
    
        average = total/i;
    
        document.getElementById("result").innerHTML = "The total of your numbers is " + total + " and their average is " + average;
    }