【发布时间】:2017-02-18 01:50:45
【问题描述】:
我正在尝试使用 for 循环来显示提示,但如何从循环中获取平均值?当它循环一个带有 propmt 的 var 时,它会像 var student[0],var student[1] 等它们是如何定义的,所以我可以平均它们吗?
<html>
<head>
<title>
for
</title>
</head>
<body>
<p>
Average your marks:
<button onclick="myFunction()">Start</button>
</p>
<script type="text/javascript">
function myFunction() {
var Avg = "";
for (var i = 0; i < 5; i++) { // this part makes the prompt below repeat 5 times
// but how would i multiply them.
var Student = prompt("Enter your mark. ");
Avg += // no clue what to do here
}
window.alert(Avg);
}
</script>
</body>
</html>
【问题讨论】:
-
我打开页面时的输入,它重复 var Student = prompt("enter your scores")。它像我想要的那样重复 5 次,但是我如何将 5 个重复的提示相乘并除以 5 得到平均值? @uzaif
-
将所有
Student值相加,然后在循环后除以5
标签: javascript html for-loop