我遇到了将数组所有元素相加并计算它们的平均值的问题。我该怎么做,并在当前代码中实现它?如下所示,这些元素应该被定义。
<script type="text/javascript">
//<![CDATA[
var i;
var elmt = new Array();
elmt[0] = "0";
elmt[1] = "1";
elmt[2] = "2";
elmt[3] = "3";
elmt[4] = "4";
elmt[5] = "7";
elmt[6] = "8";
elmt[7] = "9";
elmt[8] = "10";
elmt[9] = "11";
// Problem here
for (i = 9; i < 10; i++){
document.write("The sum of all the elements is: " + /* Problem here */ + " The average of all the elements is: " + /* Problem here */ + "<br/>");
}
//]]>
</script>
var elmt = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
这样会更好。 - James M