这是我的第一篇文章。我正在编写一个程序来从四个输入框中获取输入,找出这四个的总和并找到平均值。当我这样做时,我得到一个 NaN 错误,有人可以指出我哪里出错了。谢谢
<html>
<head>
<title> Average marks </title>
<script type = "text/javascript">
function average(form)
{
scores = new Array(4)
scores [0] = form.mark1.value
scores [0] = new Number(scores[0])
scores [1] = form.mark2.value
scores [1] = new Number(scores[1])
scores [2] = form.mark3.value
scores [2] = new Number(scores[2])
scores [3] = form.mark4.value
scores [3] = new Number(scores[3])
var Sum = 0
var average
for(var x = 0; x < scores.length; x ++)
{
Sum = Sum + scores[x]
average = Sum / scores[x]
}
document.write("The sum of the marks is equal to " + Sum + "<br>")
document.write("The average of these marks is equal to " + average + "<br>")
}
</script>
</head>
<body>
<form>
Enter the first mark : <input type = "text" name="mark1"> <br>
Enter the second mark : <input type = "text" name="mark2"> <br>
Enter the third mark : <input type = "text" name="mark3"> <br>
Enter the fourth mark : <input type = "text" name="mark4"> <br>
<input type = "submit" value = "submit" onclick="average(this.form)">
</form>
</body>
</html>