其他分享
首页 > 其他分享> > js获取整型数组最大值、最小值、平均值

js获取整型数组最大值、最小值、平均值

作者:互联网

---恢复内容开始---

let values = [];//数组(整型数字)
//获取数组最大值
function arrMaxNum(arr){
var maxNum = null;
for (var i = 0; i < arr.length; i++) {
var item =parseInt(arr[i]);
if(item){
if(!maxNum){
maxNum = item;
}
item>maxNum ? maxNum =item : null;
}
};
return maxNum;
};
//获取数组最小值(排除0)
function arrMinNum(arr){
  var minNum = null;
for (var i = 0; i < arr.length; i++) {
var item =parseInt(arr[i]);
if(item){
if(!minNum){
minNum = item;
}
item<minNum ? minNum =item : null;
}
};
return minNum;
};

//获取数组平均值(排除0)
function arrAverageNum(arr){
var sum = 0;
var count = 0
for (var i = 0; i < arr.length; i++) {
var item =parseInt(arr[i]);
if(item){
sum += item;
count++;
}
};
return ~~(sum/count*100)/100;
};

---恢复内容结束---

let values = [];//数组(整型数字)
//获取数组最大值
function arrMaxNum(arr){
var maxNum = null;
for (var i = 0; i < arr.length; i++) {
var item =parseInt(arr[i]);
if(item){
if(!maxNum){
maxNum = item;
}
item>maxNum ? maxNum =item : null;
}
};
return maxNum;
};
//获取数组最小值(排除0)
function arrMinNum(arr){
  var minNum = null;
for (var i = 0; i < arr.length; i++) {
var item =parseInt(arr[i]);
if(item){
if(!minNum){
minNum = item;
}
item<minNum ? minNum =item : null;
}
};
return minNum;
};

//获取数组平均值(排除0)
function arrAverageNum(arr){
var sum = 0;
var count = 0
for (var i = 0; i < arr.length; i++) {
var item =parseInt(arr[i]);
if(item){
sum += item;
count++;
}
};
return ~~(sum/count*100)/100;
};

标签:arr,maxNum,++,minNum,js,item,最小值,整型,var
来源: https://www.cnblogs.com/webgis-mc/p/10442924.html