<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>Document</title> </head> <script> function fun(arr){ var sum=0; var len=arr.length; //用len表示数组的长度 for(var i=0;i<len;i++) { sum=sum+arr[i]; } var pingjunzhi=sum/len; return pingjunzhi; } var a=[10,20,30,40]; //声明数组用的是中括号,不是大括号 alert(fun(a)); </script> <body> </body> </html>