//求最大值和最小值 var tmp = [1,12,"-",5]; var max = tmp[0]=="-"?0:tmp[0]; var min= tmp[0]=="-"?0:tmp[0]; for(var i=1;i<tmp.length;i++){ if(Number(max)=="-"||Number(tmp[i])=="")continue; if(Number(max)<Number(tmp[i])) max=tmp[i]; if(Number(min)>Number(tmp[i])) min=tmp[i]; } console.log(max); console.log(min); //求平均值 var arr1=[11,2,"-",5]; var getSum=function(ar){ var arr=ar var sum=arr.reduce(function(prev, curr, idx, arr){ if(curr=="-"||curr=="")return parseFloat(prev) return parseFloat(prev) + parseFlaot(curr); },0) return sum; } alert( getSum(arr1)); //11
求和
function sum(arr) { var s = 0; for (var i=arr.length-1; i>=0; i--) { if(isNaN(arr[i])){ s+=0; }else{ s += arr[i]; } } return s; }