gpt4 book ai didi

javascript - 计算数字的平均值

转载 作者:行者123 更新时间:2023-12-03 03:27:35 24 4
gpt4 key购买 nike

<!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>

/*
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;

一切看起来都在我的代码中工作,除了 JavaScript 中的第 33 行,“函数calculateAverage”似乎无法正常工作,当我单击按钮计算平均值时,我得到“NaN”作为结果,我我不确定如何解决这个问题,因此我们将不胜感激,谢谢

最佳答案

与数组 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>

关于javascript - 计算数字的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46251674/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com