这可能是一个愚蠢的问题,但我对JavaScript很新,并且不能在我的生活中弄清楚如何获得用户输入的数组元素的平均值(平均值)。这是我到目前为止编写的代码,其中提示用户输入数组中的元素数并循环返回并提示多次输入元素。任何帮助表示赞赏。感谢。
<html>
<head>
<title>Title</title>
<script type="text/javascript">
var nums = new Array();
var N = prompt("How many numbers would you like to enter? ");
N = parseInt(N);
var i = 0;
for(i = 0; i <= N - 1; i++)
{
nums[i] = prompt("Enter your numbers: ");
document.write("Number you chose: " + nums[i] + "<br />");
}
</script>
</head>
<body>
</body>
</html>
答案 0 :(得分:1)
您必须使用求和变量s
来添加输入值,并使用此公式average = s / N
计算最终的平均值:
<script type="text/javascript">
var nums = new Array();
var N = prompt("How many numbers would you like to enter? ");
N = parseInt(N);
var i = 0, s = 0;
for(i = 0; i <= N - 1; i++)
{
nums[i] = parseInt(prompt("Enter your numbers: "));
s += nums[i];
document.write("Number you chose: " + nums[i] + "<br />");
}
var ave = s / N;
document.write("Average: " + ave + "<br />");
</script>
此外,如果您不需要这些输入数字,则可以删除nums
数组并使用简单的var代替它:
<script type="text/javascript">
var nums = new Array();
var N = prompt("How many numbers would you like to enter? ");
N = parseInt(N);
var i = 0, s = 0;
for(i = 0; i <= N - 1; i++)
{
var x = parseInt(prompt("Enter your numbers: "));
s += x;
document.write("Number you chose: " + x + "<br />");
}
var ave = s / N;
document.write("Average: " + ave + "<br />");
</script>
答案 1 :(得分:0)
<script>
var nums = new Array();
var N = prompt("How many numbers would you like to enter? ");
N = parseInt(N);
var i = 0;
for (i = 0; i <= N - 1; i++) {
// convert string to integer
nums[i] = parseInt(prompt("Enter your numbers: "));
document.write("Number you chose: " + nums[i] + "<br />");
}
var sum = nums.reduce(function(prev, cur) {
return prev + cur;
}, 0);
var avg = sum / N;
console.log(avg);
</script>
答案 2 :(得分:0)
检查一下......
<title>
Title</title>
<script type="text/javascript">
function getSum(total, num) {
return total + num;
}
var nums = new Array();
var N = prompt("How many numbers would you like to enter? ");
N = parseInt(N);
var i = 0;
for (i = 0; i <= N - 1; i++) {
nums[i] = parseInt(prompt("Enter your numbers: "));
document.write("Number you chose: " + nums[i] + "<br />");
}
document.write("Avg. of entered numbers = " + (nums.reduce(getSum)) / nums.length);
</script>
<body>
</body>
答案 3 :(得分:0)
首先,您可以在for循环中定义var i = 0
,i <= N - 1
与i < N
相同。编写循环的更简单方法是.. for(var i = 0; i < N; i++)
(也从上面删除var N = 0;
)。
现在没有必要将用户的数字放入数组中以获得它们的平均值。平均所需的只是数字和数字的总和。
关于Javascript的另一个很酷的事情是,它可以自由地将您使用的变量转换为适合所用上下文的变量类型。这意味着在您的情况下不需要N = parseInt(N);
,因为它只能被视为循环中的整数。
如果您考虑上述两个注意事项,可以使用以下代码之一。
解决方案1:没有数组,只保留数字的总和和数量
//Prompt the user how many numbers they would like to enter
var N = prompt("How many numbers would you like to enter? ");
//Set sum to 0, which gives us a base to add each number to it.
var sum = 0;
//Complete this loop the same amount of times as the user entered in the prompt
for (var i = 0; i < N; i++ ) {
//Add the users response to the sum of responses
sum += parseInt(prompt("Enter your numbers: "));
}
//Write into the document the sum of
document.write( "Avg. of entered numbers = " + sum / N);
但是,如果您确实想要使用数组作为问题陈述,则可以使用以下代码。
解决方案2:数组,同时跟踪总和
//Prompt the user how many numbers they would like to enter
var N = prompt("How many numbers would you like to enter? ");
//Create an empty array to push elements to it
var nums = new Array();
//Set sum to 0, which gives us a base to add each number to it.
var sum = 0;
//Complete this loop the same amount of times as the user entered in the prompt
for (var i = 0; i < N; i++ ) {
//Get the users response to a variable
var response = parseInt(prompt("Enter your numbers: "));
//Add that variable to the sum
sum += response;
//And also push that variable to the array
nums.push(response);
}
//Write into the document the sum of the document
document.write( "Avg. of entered numbers = " + sum / nums.length);
如果您注意到我们使用了nums.length
而不是N
。如果您不确定用户将键入多少个数字,您可以使用数组的长度来查看输入的数量。
现在,这是一种更简单的方法,您可以在使用数组时执行此操作,而无需跟踪for循环中数组的总和。这利用了许多其他答案所使用的reduce函数。
解决方案3:只是一个数组!
//Prompt the user how many numbers they would like to enter
var N = prompt("How many numbers would you like to enter? ");
//Create an empty array to push elements to it
var nums = new Array();
//Complete this loop the same amount of times as the user entered in the prompt
for (var i = 0; i < N; i++) {
//Push users response to the original array
nums.push(parseInt(prompt("Enter your numbers: ")));
}
//Use the reduce function to get the sum of the array, and then divide it by the amount of elements in the array.
document.write( "Avg. of entered numddbers = " + (nums.reduce((a, b) => a + b) / nums.length));