我正在嘗試使用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