我正在尝试使用for循环来获取提示,但是如何从循环中获取平均值?当它使用propmt循环变量时,它会变成var student [0],var student [1]等等,它们是如何定义的,以便我可以对它们进行平均?for循环的求平均值
<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 =提示(“输入您的标记”)。它会重复5次,就像我想要的那样,但是如何将5次重复的提示乘以5并得到平均值? @uzaif –
将所有'Student'值加起来,然后循环后除以5 – charlietfl